• Ruby(3):基本语法中


    字符串分割成数组:
    可以使用先scan再join的方法,当然其实有更好的 split方法,专门用来分割字符串

    1 # 在Ruby中,如果不使用inspect,直接使用puts输出数组,那么每个元素会占用一行输出
    2 irb(main):001:0> puts "Short sentence.Another.No more".split(/./)
    3 Short sentence
    4 Another
    5 No more
    6 => nil
    7 irb(main):002:0> puts "Short sentence.Another.No more".split(/./).inspect
    8 ["Short sentence", "Another", "No more"]
    9 => nil
    Split

     
    数组迭代:
    可以直接使用each方法来对每个元素进行遍历输出:

     1 irb(main):005:0> a = [1, 2, 3, 4]
     2 => [1, 2, 3, 4]
     3 # collection对每一个元素进行迭代,返回的结果作为新的数组结果
     4 irb(main):009:0> b = a.collect do |element| element*2 end
     5 => [2, 4, 6, 8]
     6 irb(main):010:0> b
     7 => [2, 4, 6, 8]
     8 # 如果不对元素进行任何操作,则返回的为同样个数每个元素为nil的数组
     9 irb(main):011:0> b = a.collect do |element| puts element end
    10 1
    11 2
    12 3
    13 4
    14 => [nil, nil, nil, nil]
    15 irb(main):012:0> b
    16 => [nil, nil, nil, nil]
    17 # each永远返回原来的数组,没法改变原来的数组
    18 irb(main):013:0> b = a.each do |element| end
    19 => [1, 2, 3, 4]
    20 irb(main):014:0> b
    21 => [1, 2, 3, 4]
    22 irb(main):015:0> b = a.each do |element| element*3  end
    23 => [1, 2, 3, 4]
    24 irb(main):016:0> b
    25 => [1, 2, 3, 4]
    26 # each一般用来进行相关操作
    27 irb(main):018:0> a.each do |element| puts element end
    28 1
    29 2
    30 3
    31 4
    32 => [1, 2, 3, 4]
    ArrayIterator

    ruby中的数组声明同java,最后一个,(逗号) 可要可不要
     
    数组的其他操作:

     1 irb(main):019:0> b = [2, 3, 4, 5]
     2 => [2, 3, 4, 5]
     3 # 数组支持 + -,相当于Redis中的UNION和DIFF。返回操作之后的数组,原数组不会变
     4 irb(main):020:0> a - b
     5 => [1]
     6 # 判断是否为空
     7 irb(main):021:0> puts "a is not empty" unless a.empty?
     8 a is not empty
     9 => nil
    10 # 判断是否包含某元素
    11 irb(main):022:0> a.include?(3)
    12 => true
    13 # 返回第一个元素
    14 irb(main):023:0> a.first
    15 => 1
    16 # 返回最后一个元素
    17 irb(main):024:0> a.last
    18 => 4
    19 # 返回开始的3个元素
    20 irb(main):025:0> a.first(3)
    21 => [1, 2, 3]
    22 irb(main):026:0> a.last(3)
    23 => [2, 3, 4]
    24 # 逆序
    25 irb(main):027:0> a.reverse
    26 => [4, 3, 2, 1]
    27 # 原数组不会变
    28 irb(main):028:0> a
    29 => [1, 2, 3, 4]
    OtherOp

    散列表:

     1 # 基本形式
     2 irb(main):029:0> dict = {'cat'=>'cat1', 'dog'=>'dog1'}
     3 => {"cat"=>"cat1", "dog"=>"dog1"}
     4 # key必须为字符串或者标志
     5 irb(main):030:0> dict2 = {cat=>cat1}
     6 NameError: undefined local variable or method `cat' for main:Object
     7 Did you mean?  catch
     8     from (irb):30
     9     from /usr/bin/irb:11:in `<main>'
    10 irb(main):031:0> dict2 = {'cat'=>cat1}
    11 NameError: undefined local variable or method `cat1' for main:Object
    12 Did you mean?  catch
    13     from (irb):31
    14     from /usr/bin/irb:11:in `<main>'
    15 irb(main):032:0> dict2 = {:cat=>'cat1'}
    16 => {:cat=>"cat1"}
    17 # length和size都可以得到长度
    18 irb(main):033:0> dict.length
    19 => 2
    20 irb(main):034:0> dict.size
    21 => 2
    22 irb(main):035:0> dict['cat']
    23 => "cat1"
    24 # 不支持.访问
    25 irb(main):036:0> dict.cat
    26 NoMethodError: undefined method `cat' for {"cat"=>"cat1", "dog"=>"dog1"}:Hash
    27     from (irb):36
    28     from /usr/bin/irb:11:in `<main>'
    Hash

     
    迭代散列表:

    1 irb(main):037:0> dict.each do |key, value| puts "#{key} matches #{value}" end
    2 cat matches cat1
    3 dog matches dog1
    4 => {"cat"=>"cat1", "dog"=>"dog1"}
    Iterator

     
    得到散列中的所有键和值:

    1 irb(main):038:0> dict.keys.inspect
    2 => "["cat", "dog"]"
    3 irb(main):039:0> dict.values.inspect
    4 => "["cat1", "dog1"]"
    KeysValues

     
    删除散列中的元素:

     1 # 直接删除
     2 => "["cat1", "dog1"]"
     3 irb(main):040:0> dict.delete('cat')
     4 => "cat1"
     5 irb(main):041:0> dict
     6 => {"dog"=>"dog1"}
     7 # 有条件的删除
     8 irb(main):042:0> dict.delete_if do |key, value| value == 'dog1' end
     9 => {}
    10 irb(main):043:0> dict
    11 => {}
    Delete 

    添加元素:

    可以直接每次使用中括号对属性进行赋值:

    a['key'] = 'value';

    也可以可以利用merge小技巧

     1 irb(main):053:0> dict.merge({'dog'=>'dog1'})
     2 => {"dog"=>"dog1"}
     3 irb(main):054:0> dict.merge({'cat'=>'cat1', 'newDog'=>'dog2', 'newCat'=>'cat2'}) 
     4 => {"cat"=>"cat1", "newDog"=>"dog2", "newCat"=>"cat2"}
     5 # 发现merge只是返回一个新的hash,不能改变原来的hash
     6 irb(main):055:0> dict
     7 => {}
     8 # 可以通过下面的方式巧妙的添加元素:
     9 irb(main):057:0> dict = dict.merge({'cat'=>'cat1', 'newDog'=>'dog2', 'newCat'=>'cat2'})
    10 => {"cat"=>"cat1", "newDog"=>"dog2", "newCat"=>"cat2"}
    11 irb(main):058:0> dict
    12 => {"cat"=>"cat1", "newDog"=>"dog2", "newCat"=>"cat2"}
    Add

    散列表中可以嵌套散列表,我们可以通过多重key值进行访问

    1 # 散列中的元素也可以是散列值
    2 irb(main):059:0> dict = dict.merge({'animal'=>{'insideCat'=>'cat3'}})
    3 => {"cat"=>"cat1", "newDog"=>"dog2", "newCat"=>"cat2", "animal"=>{"insideCat"=>"cat3"}}
    4 irb(main):060:0> dict['animal']
    5 => {"insideCat"=>"cat3"}
    6 # 可以嵌套获取值
    7 irb(main):061:0> dict['animal']['insideCat']
    8 => "cat3"
    InsideHash

    流程控制:
    if 、unless 
    if elsif 
    case:

     1 fruit = 'orange'
     2 irb(main):084:0> case fruit
     3 irb(main):085:1> when 'orange'
     4 irb(main):086:1> color = 'orange'
     5 irb(main):087:1> when 'apple'
     6 irb(main):088:1> color = 'green'
     7 irb(main):089:1> else
     8 irb(main):090:1* color = 'unknown'
     9 irb(main):091:1> end
    10 => "orange"
    11 
    12 # 上例可以简化为
    13 irb(main):092:0> color = case fruit
    14 irb(main):093:1> when 'orange'
    15 irb(main):094:1> 'orange'
    16 irb(main):095:1> else 'unknown' end
    17 => "orange"
    18 irb(main):096:0> color
    19 => "orange"
    Case

    while
    until:与while相反,直到条件满足才跳出循环

    1 irb(main):097:0> x = 1
    2 => 1
    3 irb(main):098:0> until x > 99
    4 irb(main):099:1> puts x 
    5 irb(main):100:1> x = x * 20
    6 irb(main):101:1> end
    7 1
    8 20
    9 => nil
    Until
  • 相关阅读:
    不要自称为程序员(转)
    JSON数据格式
    netstat
    win7,ubuntu双系统——重装win7后如何恢复ubuntu引导
    js中setInterval与setTimeout用法
    使用CSS中margin和padding的基础和注意事项
    HTTP协议与HTML表单(再谈GET与POST的区别)
    (转)JAVA AJAX教程第四章—AJAX和MVC的简单结合
    (转)JAVA AJAX教程第三章—AJAX详细讲解
    制作SD卡启动自己编译的uboot.bin
  • 原文地址:https://www.cnblogs.com/Kidezyq/p/5767764.html
Copyright © 2020-2023  润新知