• Ruby语法基础(一)


    Ruby语法基础(一)

    Ruby是一种开源的面向对象程序设计的服务器端脚本语言,最初由松本行弘(Matz)设计开发,追求『快乐和生产力』,程序员友好型,被称为『human-oriented language』

    hello world

    首先打开vim

    $ vim hello.rb
    

    编写helloworld代码

    puts "hello world!"
    

    保存后运行

    $ ruby hello.rb
    hello world! # 运行结果
    

    ruby也可以在命令行中键入irb,打开命令行交互模式,类似中Python的shell

    Here Document

    puts <<EOF
      这是一种here document
    是一种多行字符串
    可以用来写大段文档
    EOF
    

    Begin&END

    puts '这是主Ruby脚本'
    
    # end语句会在程序的结尾被调用
    END{
      puts '停止Ruby程序'
    }
    
    # begin语句会在程序的开始被调用
    BEGIN{
      puts '初始化Ruby程序'
    }
    

    注释

    =begin
    单行注释用#
    多行注释用=begin .... =end
    =end
    puts '测试注释'
    

    基本数据类型

    数值类型Number

    a = 123_123 # 十进制可以加下划线
    b = 0x377 # 十六进制表示
    c  =0b1011 # 二进制表示
    d = 6.32E-32 # 科学记数法表示
    puts a, b, c, d
    

    字符串类型(String)

    • 单引号中的内容不会被转义
    • 双引号中的内容会被转义,#{}内的脚本会被执行
    name = 'world'
    puts 'hello	name'
    puts "hello	#{name.upcase}"
    puts "24*20*35=#{24*20+35}" # 计算并转成字符串
    
    运行结果:
    hello	name
    hello	WORLD
    24*20*35=515
    

    #{ expr }可以执行expr脚本并转换成字符串

    数组(Array)

    类似于Python中的list,支持[]索引,插入,删除,替换,集合间运算

    a = ['fred', 
         10,
         3.14,
         'String',
         'last'
    ]
    a.each do |i|
      print i,"!
    "
    end
    

    哈希(Hash or Dictionary)

    hsh = {
        :red=>0xf00, # 字符串作为key的情况下,用symbol比较好
        :green => 0x0f0,
        :blue => 0x00f
    }
    hsh.each do |key, value|
      print key, ' is ', value, "
    "
    end
    

    范围(Range)

    (10..15).each do
      |i|
      print i, ' '
    end
    

    类和对象

    # 定义类
    class Customer
      # 类变量
      @@nu_of_customer = 0
      def initialize(id, name, addr)
      #   属于实例本身的实例变量
        @cust_id = id
        @cust_name = name
        @cust_addr = addr
        @@nu_of_customer += 1
      end
      # 显示细节的函数
      def show
        puts "Customer id #{@cust_id}"
        puts "Customer name #{@cust_name}"
        puts "Customer addr #{@cust_addr}"
      end
      def show_total
        puts "Total number of customers: #@@nu_of_customer"
      end
    end
    
    # 创建对象
    cust1 = Customer.new('1','John','Wisdom Apartments, Ludhiya')
    cust2 = Customer.new('2','Poul','New Empire road, Khandala')
    
    # 调用方法
    cust1.show
    cust2.show
    cust1.show_total
    
    运行结果
    Customer id 1
    Customer name John
    Customer addr Wisdom Apartments, Ludhiya
    Customer id 2
    Customer name Poul
    Customer addr New Empire road, Khandala
    Total number of customers: 2
    

    变量

    全局变量

    以$开头,不建议使用

    def f1
      $global_variable += 1
      puts "f1: $global_variable = #{$global_variable}"
    end
    
    # 全局变量用$开头
    $global_variable = 0
    
    puts "main: $global_variable = #{$global_variable}"
    f1
    puts "main: $global_variable = #{$global_variable}"
    
    运算结果
    main: $global_variable = 0
    f1: $global_variable = 1
    main: $global_variable = 1
    

    实例变量

    以@开头,属于实例本身,另外ruby体现数据与操作结合,外部不可以直接修改实例变量本身,博主认为这个面向对象很彻底

    class Sample
      def initialize
        @x = 0 # 初始化后使用
      end
      def get_x
        @x
      end
      def set_x(x)
        @x = x
      end
    end
    
    s = Sample.new
    s.set_x 2 # 命令行的传参风格
    puts s.get_x
    

    类变量

    类变量以@@开头,必须初始化后使用。类变量在定义它的类或模块的子类的子类或子模块中可以共享,例子上面讲类和对象时已经指出

    局部变量

    局部变量以小写字母或者_开头,生命周期从class、module、def或do到相应的end,或者两个括号之间。一旦出作用域就直接释放

    def sample_func
      x = 1
      puts x
    end
    sample_func
    # puts x 报错undefined local variable or method `x' for main:Object (NameError)
    

    常量

    • 常量用大写字母开头
    • 定义在类或模块内的常数可以从类或模块的内部访问,定义在类或模块外的常量可以被全局访问
    • 常量不能定义在方法内部
    class Example
      VAR1 = 100
      VAR2 = 200
      def show
        puts "VAR1=#{VAR1}"
        puts "VAR1=#{VAR2}"
      end
    end
    obj = Example.new
    obj.show
    
    运行结果
    VAR1=100
    VAR1=200
    

    伪变量

    类似于pyhton或PHP中的魔法常量,只能读不能写

    • self: 当前方法的接收器对象。
    • true:代表 true 的值。
    • false: 代表 false 的值。
    • nil: 代表 undefined 的值。
    • __FILE__: 当前源文件的名称。
    • __LINE__: 当前行在源文件中的编号

    小结

    本文也就对于ruby有了快速的感性认识,进一步的学习还要继续学习

    参考教程菜鸟教程

  • 相关阅读:
    HDU
    01字典树模板
    扩展欧几里得和乘法逆元
    HDOJ-1156 Brownie Points II 线段树/树状数组(模板)
    CF-825E Minimal Labels 反向拓扑排序
    CF-831D Office Keys 思维题
    RMQ 解决区间查询问题
    hdu 5073 有坑+方差贪心
    hdu 5074 相邻数和最大dp
    hdu 5078 水题
  • 原文地址:https://www.cnblogs.com/fanghao/p/7843121.html
Copyright © 2020-2023  润新知