美文网首页
ruby基础用法简单整理

ruby基础用法简单整理

作者: owlwisp | 来源:发表于2016-06-12 15:09 被阅读202次

ruby基础用法简单整理

基础变量部分

  1. 变量声明
    <pre> a = 10 a = "string"</pre>

  2. 支持并行赋值
    <pre> a,b = 3,5 a,b = 3, "5"
    a = b = 3</pre>

  3. 变量操作
    <pre> a += 1
    没有++操作符号</pre>

  4. 变量交换
    <pre> a,b = 3,5
    a,b = b,a #=> 5,3</pre>

  5. 语句后面不跟";"
    <pre> a = 10</pre>


  6. <pre> -5 / 2 #=> -3
    -5.0/ 2 #=> -2.5</pre>


  7. <pre> -5 % 2 #=> 1
    -5 % 2.2 #=> 1.6</pre>

字符串

  1. 声明
    <pre> str = "string"</pre>

  2. 切片操作
    <pre> str[1...3] #=> "tr"
    str[1,3] #=> "tri"</pre>

  3. 倒叙
    <pre> str[-1] #=> "g"</pre>

  4. 字符串比较
    <pre> if(str == sub) #=> true</pre>

  5. 字符替换
    <pre> str[3] = "1" #=> "str1ng"</pre>

  6. 字符串*
    <pre> str *3 #=> "stringstringstring"</pre>

  7. 字符串+
    <pre> str + "s" #=> "str1ngs"</pre>

  8. 字符串长度
    <pre> str.length str.size str.bytesize #=> 7</pre>

  9. 汉字
    <pre> str = "人"
    str.length str.size #=> 1
    str.bytesize #=> 1</pre>

  10. 数字转字符串
    <pre> str = "a is "
    a = 10.0
    str + a.to_s #=> "a is 10.0"</pre>

  11. 数组<<
    <pre> str << "es" #=> stringes
    str << ?5 #=> string5
    str << 5 #=> string�</pre>

  12. 字符串内复值
    <pre> sum = 5
    str = "string #{sum}" #=> "string 5"
    str = "string #{sum} is %d %s" % [5,"sum"] #=> "string 5 is 5 sum"</pre>

数组

  1. 声明
    <pre> arr = [1,2,3,4]
    other = 1,2,3,4
    a,b,c = [1,2,3] #=> a = 1 b =2 c =3
    a ,*b= [1,2,3,4] #=> a =1 b = [2,3,4]
    *a ,b= [1,2,3,4] #=> a =[1,2,3] b = 4</pre>

  2. 支持多元数组
    <pre> other = [1,2,3,4,"str"]</pre>

  3. 数组下标起始位置
    <pre> arr[0] #=> 1</pre>

  4. 数组切片
    <pre> sub = arr[1...3] #=> 1,2 [1,3)
    sub = arr[1,3] #=> 1,2,3 [1, =>3长度</pre>

  5. 倒叙
    <pre> arr[-1] #=> 4</pre>

  6. 数组比较
    <pre> sub = [1,2,3,4]
    if(arr == sub) #=> true</pre>

  7. 数组元素替换
    <pre> arr[2] = "d" #=> [1,2,"d",4]
    arr[1,3] = ["a","b"] #=> [1, "a", "b"]</pre>

  8. 数组*
    <pre> arr * 3 #=> [1,2,3,4,1,2,3,4,1,2,3,4]</pre>

  9. 数组+
    <pre> arr + [5] #=> [1,2,3,4,5]
    arr + [[5,6]] #=> [1, 2, 3, 4, [5, 6]]</pre>

  10. 数组长度
    <pre> arr.length arr.size #=> 74</pre>

  11. 数组= #=>两数组维持同一份拷贝
    <pre> sub = arr #=> [1,2,3,4]
    arr[2] = "d" sub #=> [1,2,d,4]</pre>

  12. Array数组深拷贝
    <pre> sub = Array.new(arr)
    if(sub == arr) #=> true
    arr[2] = "d" sub #=> [1,2,3,4]</pre>

  13. 越界操作方式
    <pre> arr[6] = 5 #=> [1, 2, 3, 4, nil, nil, 5]</pre>

  14. range对数组的初始化
    <pre> ('1'...'5').to_a #=> ["1", "2", "3", "4"]</pre>

  15. 切片赋值
    <pre> arr[1...2] = ["a"] arr #=> [1, "a", 3, 4]
    arr[1,2] = ["a"] arr #=> [1, "a", 4]</pre>

  16. 数组减法
    <pre> sub = [1,2,3,4,5,6]
    sub - arr #=> [5, 6]</pre>

  17. 数组<<
    <pre> arr << 5 #=> [1, 2, 3, 4, 5]</pre>

  18. 数组&
    <pre> sub = [1,3,5]
    arr & sub #=> [1, 3]</pre>

  19. 数组|
    <pre> sub = [1,3,5]
    arr | sub #=> [1, 2, 3, 4, 5]
    sub | arr #=> [1, 3, 5, 2, 4]</pre>

  20. 数组遍历
    <pre> arr.each {|x| print x} #=> 1234</pre>

hash

  1. 初始化
    <pre> nums = {:one=>1, :two=>2,:three=>3} #=> {:one=>1, :two=>2, :three=>3}
    sums = hash.new
    sums[1] = 1 #=> {1=>1}
    sums = {"1"=>1, 2=>2} #=> {"1"=>1, 2=>2}</pre>

  2. 支持多元hash
    <pre> nums = {:one=>1, :two=>"2"} #=> {:one=>1, :two=>"2"}</pre>

  3. 获取hash元素
    <pre> nums[:one] #=> 1</pre>

  4. 元素替换
    <pre> nums[:one] = 3 #=> {:one=>3, :two=>2}</pre>

  5. hash长度
    <pre> nums.length nums.size #=> 3</pre>

range

  1. 初始化
    <pre> range = 1..100 #=> 1..100</pre>

  2. include? && member? && cover? 判断
    <pre> range.include?10.0 #=> true
    range.include?10 #=> true
    range.include?101.0 #=> false</pre>

符号

  1. respond_to?
    <pre> class Greeter
    def add x, y
    x + y
    end
    end

    gt = Greeter.new
    gt.respond_to?:add #=> true</pre>

  2. string转符号
    <pre> puts str.to_sym str.intern #=> add</pre>

  3. 符号转string
    <pre> sign = :add
    sign.to_sym sign.id2name #=> add</pre>

  4. instance_of? is_a? kind_of?
    <pre> gt = Greeter.new
    gt.instance_of? Greeter #=> true
    gt.is_a? Greeter #=> true
    gt.kind_of? Greeter #=> true</pre>

  5. class
    <pre> gt.class #=> Greeter</pre>

  6. ==
    <pre> a = "Ruby"
    b = a
    c = "Ruby"

    a == b #=> true
    a == c #=> true
    a[1] = "3"
    a== b #=> true</pre>

  7. equal?
    <pre> a.equal?b #=> true
    a.equal?c #=> false

    a[1] = "3"
    a.equal?b #=> true</pre>

  8. eql?
    <pre> a.equal?b #=> true
    a.equal?c #=> true

    a[1] = "3"
    a.eql?b #=> true</pre>

  9. <=>
    <pre> 1 <=> 3 #=> -1
    3 <=> 3 #=> 0
    4 <=> 3 #=> 1</pre>

条件式

  1. if条件
    <pre> if a == b
    "code"
    end

    if "expr"
    "code"
    elsif "expr"
    "code"
    else
    "code"
    end

    if "expr" then
    "code"
    end

    "code" if "expr" #不允许有elsif else 等从句</pre>

  2. unless
    <pre> unless "expr"
    "code"
    end

    "code" unless "expr" #不允许有elsif else 等从句</pre>

  3. case
    <pre> case
    when "expr" then "code"
    when "expr" then "code"
    when "expr" then "code"
    else "many" then "code"
    end</pre>

  4. until while
    <pre> until "expr" do
    "code"
    end

    "code" while "expr"</pre>

  5. for
    <pre> for a in array
    "code"
    end </pre>

  6. times
    <pre> 3.times "code" #=>0,1,2</pre>

  7. each
    <pre> data.each{|x| puts x}</pre>

  8. map
    <pre> [1,2,3,4].map {|x| puts x}</pre>

  9. upto/downto
    <pre> 4.upto(7) { |x| print x} #=> 4567</pre>

  10. inject
    <pre> sum = data.inject {|result , x| x +result} #=> 10</pre>

  11. yield
    <pre> def five
    yield 1,2,3,4,5
    end

    five do |x,*y,z|
    print x #=> 1
    print y #=> [2,3,4]
    print z #=> 5
    end</pre>

方法

  1. 函数
    <pre> def func x
    return x
    end</pre>

  2. 多返回值
    <pre> def func x
    x
    end

    def func
    return 1 , 2
    end

    def func
    [1 , 2]
    end</pre>

  3. 单键方法
    <pre> o = "message"
    def o.func x
    x
    end</pre>

  4. 可以定贷带问号结尾的函数
    <pre> def empty
    "code"
    end</pre>

  5. 可以用变参
    <pre> def max(first, *res)
    max = first
    res.each{
    |x| max = x if x > max
    }
    max
    end
    puts max 1,2,3,4,5 #=> 5</pre>

  6. 默认参数
    <pre> def sum x,y = 2,z = 3
    x + y + z
    end</pre>

  7. hash函数
    <pre> def sequence args
    n = args[:n]
    m = args[:m]
    c = args[:c]
    a = []
    n.times {|i| a << m*i+c}
    a
    end

    puts sequence({:n=>3,:m=>5,:c=>1})
    puts sequence :n=>3,:m=>5,:c=>1 (裸hash)</pre>

  8. 代码块
    <pre> def sequence n ,m ,c
    i = 0
    while i < n
    yield i * m + c
    i+= 1
    end
    end

    sequence(5,2,2) {|x| puts x } #=> 2,4,6,8,10</pre>

  9. proc对象
    <pre> def makeProc &block
    block
    end

    block = makeProc {|x| puts x }
    block.call(3) #=> 3</pre>

  10. proc.new
    <pre> block = Proc.new {|x| puts x }
    block.call(3) #=> 3</pre>

  11. lambda表达式
    <pre> lambda = ->x{puts x }
    lambda.call(3) #=> 3</pre>

  12. lambda表达式默认参数
    <pre> lambda = ->x =3{ y = x + 1; puts y }
    lambda.call #=> 4</pre>

  1. 声明(类名必须大写否者报错)
    <pre> class Point
    def initialize(x,y)
    @x,@y = x,y
    end

     def x; @x; end
     def y; @y; end
    
     def x=value; @x = value; end
     def y=value; @y = value; end
    

    end

    p = Point.new 3,5</pre>

  2. 枚举坐标值
    <pre> class Point
    def initialize(x,y)
    @x,@y = x,y
    end

     def each
       yield @x
       yield @y
     end
    

    end

    p = Point.new 3,5
    p.each {|x| print x} #=>3,5</pre>

  3. 定义==
    <pre> def == o
    if o.is_a?Point
    @x ==o.x && @y == o.y
    elsif
    false
    end
    end</pre>

  4. 定义严格版eql?
    <pre> def eql? o
    if o.instance_of?Point
    @x.eql?(o.x) && @y.eql?(o.y)
    elsif
    false
    end
    end</pre>

  5. 读写性
    <pre> attr_reader :x,:y 只读
    attr_accessor :x,:y 读写</pre>

  6. Struct方法创建类
    <pre> Poi = Struct.new(:x,:y)
    po = Poi.new(3,5)
    puts po.x</pre>

  7. 拥有private,public,protected的类可见性
    <pre></pre>

  8. 单例方法
    <pre> o = Point.new 3,5
    def o.sayBye
    puts "byebye!"
    end

    o.sayBye #=> "byebye!"</pre>

  9. 单利方法的另外一种模式
    <pre> class << o
    def sayHi
    puts "Hi"
    end
    end</pre>

  10. 单例方法查询
    <pre> puts o.singleton_methods #=> sayBye</pre>

  11. 使用self定义的静态方法
    <pre> class SelfTest
    def self.test
    puts "hello world selfTest."
    end
    end

    SelfTest.test #=> "hello world selfTest."</pre>

  12. 使用类名定义的静态方法
    <pre> class SelfTest
    def SelfTest.test
    puts "hello world selfTest."
    end
    end

    SelfTest.test #=> "hello world selfTest."</pre>

  13. send调用方法
    <pre> class Base
    attr_accessor :x

    def initialize x
      @x = x
    end
    
    def add x ,y
      x + y
    end
    

    end

    o = Base.new 3
    puts o.send(:add,3,7) #=> 10</pre>

  14. module
    <pre> module Model
    def sayHello
    puts "hello world!"
    end
    end

    class Base
    include Model

    def initialize x
      @x = x
    end
    

    end

    o = Base.new
    o.sayHello #=> "hello world!"</pre>

反射,元编成

  1. class superclass 反射
    <pre> class Base
    attr_accessor :x

     def initialize x
       @x = x
     end
    
     def log
       puts @x
     end
    

    end

    o = Base.new 3
    puts o.class #=> Base
    puts o.class.superclass #=> Object</pre>

  2. eval求值
    <pre> a = 100
    puts eval "a +1"</pre>

正则表达式

<pre> if "Ruby" =~ /[R,r]uby/
puts "true"
end</pre>

集合

  1. 遍历切片遍历
    <pre> (1...10).each_slice(3) {|x| print x} #=> [1, 2, 3][4, 5, 6][7, 8, 9]</pre>

  2. 滑动切片遍历
    <pre> (1...10).each_cons(3) {|x| print x} #=> [1, 2, 3][2, 3, 4][3, 4, 5][4, 5, 6][5, 6, 7][6, 7, 8][7, 8, 9]
    </pre>

相关文章

网友评论

      本文标题:ruby基础用法简单整理

      本文链接:https://www.haomeiwen.com/subject/kriedttx.html