ruby基础用法简单整理

Ruby语法整理


1.基础变量部分
1.变量声明
a = 10  a = "string"
  
  2.支持并行赋值
a,b = 3,5 a,b = 3, "5"
    a = b= 3
 
3.变量操作
a += 1
没有++操作符号


4.变量交换
  a,b = 3,5
a,b = b,a #=> 5,3
  
5.语句后面不跟;
    a = 10 
     
  6. /
    -5 / 2   #=> -3
        -5.0/ 2 #=> -2.5
        
  7.%
    -5 % 2 #=> 1
        -5 % 2.2 #=> 1.6
  
2.字符串
  1.声明
    str = "string"
     
    2.切片操作
    str[0...5] #=> "strin"
     
    3.倒叙
      str[-1] #=> "g"
      
    4.字符串比较
    if(str == sub) #=> true
     
    5.字符替换
    str[3] = "1"  #=> "str1ng"
     
    6.字符串*
    str * 3 #=> "stringstringstring"
   
    7.字符串+
    str + "s" #=> "str1ngs"
     
    9.字符串长度
    str.length  str.size str.bytesize #=> 7
     
    10.汉字
    str = "人"
      str.length  str.size #=> 1
    str.bytesize #=> 1
     
    11.数字转字符串
    str = "a is "
      a = 10.0
    str + a.to_s #=> "a is 10.0"
     
    12.<<
    str << "es" #=> stringes
      str << ?5   #=> string5
      str << 5 #=> string
       
    13.字符串内复值
    sum = 5
      str = "string #{sum}"   #=>  "string 5"
str = "string #{sum}  is %d %s" % [5,"sum"]    #=> "string 5  is 5 sum"
  
     
2.数组
1.声明
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
  
2.支持多元数组
  other = [1,2,3,4,"str"]


3.数组下标起始位置
  arr[0] #=> 1
   
  4.数组切片
  sub = arr[1...3] #=>  1,2  [1,3)
  sub = arr[1,3] #=>  1,2,3 [1, =>3长度
   
  5.倒叙
    arr[-1] #=> 4
    
  6.数组比较
  sub = [1,2,3,4]
  if(arr == sub) #=> true
   
  5.字符替换
  arr[2] = "d"  #=> [1,2,"d",4]
    arr[1,3] = ["a","b"]   #=> [1, "a", "b"]
    
  6.数组*
  arr * 3 #=> [1,2,3,4,1,2,3,4,1,2,3,4]
 
  7.数组+
  arr + [5] #=> [1,2,3,4,5]
    arr + [[5,6]] #=> [1, 2, 3, 4, [5, 6]]
   
  9.数组长度
  arr.length  arr.size  #=> 74
   
  10.数组= 两数组维持同一份拷贝
  sub = arr #=> [1,2,3,4]
    arr[2] = "d" sub #=> [1,2,d,4]
    
  11.Array数组深拷贝
  sub = Array.new(arr)
    if(sub == arr) #=> true
    arr[2] = "d"  sub #=> [1,2,3,4]
    
  12.越界操作方式
  arr[6] = 5  #=> [1, 2, 3, 4, nil, nil, 5]
   
  13.range初始化
  ('1'...'5').to_a #=> ["1", "2", "3", "4"]
   
  14.切片负值
  arr[1...2] = ["a"]  arr #=> [1, "a", 3, 4]
    arr[1,2] = ["a"]  arr #=> [1, "a", 4]
    
  15.数组减法
  sub = [1,2,3,4,5,6]
    sub - arr    #=> [5, 6]
   
  16.数组<<
  arr << 5   #=> [1, 2, 3, 4, 5]
   
  17.数组&
  sub = [1,3,5]
    arr & sub   #=> [1, 3]
   
  18.数组|
      sub = [1,3,5]
    arr | sub   #=> [1, 2, 3, 4, 5]
    sub | arr #=> [1, 3, 5, 2, 4]
     
  19.数组遍历
  arr.each {|x| print x}     #=>  1234
  
3.hash
1.初始化
  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}
      
    2.支持多元hash
    nums = {:one=>1, :two=>"2"}  #=>  {:one=>1, :two=>"2"}
  
  3.获取hash元素
    nums[:one] #=> 1
     
    4.元素替换
    nums[:one] = 3  #=> {:one=>3, :two=>2}


    5.hash长度
    nums.length  nums.size  #=> 3
     
4.ranges
1.初始化
range = 1..100  #=> 1..100
      
    2.include? && member? && cover? 判断
    range.include?10.0 #=> true
      range.include?10 #=> true
      range.include?101.0 #=> false
       
5.符号
1.respond_to?
class Greeter
def add x, y
x + y
end
end


gt = Greeter.new 
gt.respond_to?:add #=> true
  
  2.string转符号
    puts str.to_sym str.intern #=> add
    
    3.符号转string
    sign = :add
sign.to_sym sign.id2name #=> add
 
  4.instance_of?  is_a?  kind_of? 
    gt = Greeter.new
gt.instance_of? Greeter   #=> true
  gt.is_a? Greeter   #=> true
    gt.kind_of? Greeter   #=> true
  
  5.class
    gt.class #=> Greeter
     
    6.== 
    a = "Ruby"
      b = a
      c = "Ruby"
       
        a == b  #=> true
        a == c  #=> true
        a[1] = "3"
a== b #=> true
  
    7.equal?
      a.equal?b     #=> true
      a.equal?c   #=> false
       
        a[1] = "3"
a.equal?b #=> true
  
    8.eql?
    a.equal?b     #=> true
      a.equal?c   #=> true
       
    a[1] = "3"
a.eql?b #=> true
  
  9.<=>
    1 <=> 3 #=> -1
 3 <=> 3 #=> 0
 4 <=> 3 #=> 1
  
6.对象


7.条件式
  1.if条件
    if a == b
      "code"
    end 

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


    if "expr" then
      "code"
    end
    
    "code" if  "expr"   #不允许有elsif else 等从句
  
  2.unless
    unless "expr" 
      "code"
    end 
    
    "code" unless  "expr"   #不允许有elsif else 等从句
    
  3.case
    case
    when "expr" then "code"
    when "expr" then "code"
    when "expr" then "code"
    else "many" then "code"
    end
    
  4.until while
  
    until "expr" do
      "code"
    end
    
    "code" while "expr"
  
  5.for
    for a in array 
      "code"
    end  
    
  6.times
    3.times "code"  #=>0,1,2
  
  7.each
    data.each{|x| puts x}
    
  8.map 
    [1,2,3,4].map {|x| puts x}
    
  9.upto/downto
    4.upto(7) { |x| print x}   #=> 4567
    
  10.inject
    sum = data.inject {|result , x| x +result} #=> 10
    
  11.yield 
    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
    
8.方法
  1.函数
    def func x
      return x
    end
    
  2.多返回值
  
    def func x
      x
    end
    
    def func
      return 1 , 2
    end
    
    def func
      [1 , 2]
    end
    
  3.单键方法
    o = "message"
    def o.func x
      x
    end
    
  4.可以定义?结尾的函数
    def empty
      "code"
    end
    
  5.可以变参数
    def max(first, *res)
      max = first
      res.each{
        |x| max = x if x > max
      }
      max
    end
    puts max 1,2,3,4,5  #=> 5
    
  6.默认参数
    def sum x,y = 2,z = 3
      x + y + z
    end
  
  7.hash 函数
  
    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)
  
  8.代码块
    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
    
  9.Proc对象
    def makeProc &block
      block
    end


    block = makeProc {|x| puts x }  
    block.call(3)                     #=> 3
    
  10.Proc.new
    block = Proc.new {|x| puts x }
    block.call(3)                     #=> 3
    
  11.lambda表达式
    lambda = ->x{puts x }
    lambda.call(3)                    #=> 3
  
  12.lambda表达式默认参数
    lambda = ->x =3{ y = x + 1; puts y }
    lambda.call           #=> 4
    
9.类
  1.声明(类名必须大写否者报错)
    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
    
  2.枚举坐标值
    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
  
  3.定义==
    def == o
      if o.is_a?Point
        @x ==o.x && @y == o.y
      elsif
        false
      end
    end
  
  4.定义严格版eql?
    def eql? o
      if o.instance_of?Point
        @x.eql?(o.x) && @y.eql?(o.y)
      elsif 
        false
      end
    end
  
  5.读写性
    attr_reader :x,:y  只读
    attr_accessor :x,:y  读写
      
  6.Struct方法创建类
    Poi = Struct.new(:x,:y)
    po = Poi.new(3,5)
    puts po.x
  
  7.拥有private,public protected的类可见性
  
  8.单例方法
    o = Point.new 3,5
    def o.sayBye
      puts "byebye!"
    end
    
    o.sayBye   #=> "byebye!"
    
  9.单利方法的另外一种模式
    class << o
      def sayHi
        puts "Hi"
      end
    end
    
  10.单例方法查询
    puts o.singleton_methods  #=> sayBye
  
  11.使用self定义的静态方法
    class SelfTest
      def self.test
        puts "hello world selfTest."
      end
    end
    
    SelfTest.test  #=> "hello world selfTest."
  
  12.使用类名定义的静态方法
    class SelfTest
      def SelfTest.test
        puts "hello world selfTest."
      end
    end
    
    SelfTest.test  #=> "hello world selfTest."
    
  13.send调用方法
    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
    
  14.module
    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!"
    
  
10.反射,元编成
  1. class superclass 反射
    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
    
  2.eval求值
    a = 100
    puts eval "a +1"
    
11.正则表达式
  if "Ruby" =~ /[R,r]uby/
    puts "true"

  end


12.集合
  1.遍历 切片遍历和滑动切片遍历
    (1...10).each_slice(3) {|x| print x} #=> [1, 2, 3][4, 5, 6][7, 8, 9]
    (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]
    
    
    
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值