字符串分割成数组:
可以使用先scan再join的方法,当然其实有更好的 split方法,专门用来分割字符串1 # 在Ruby中,如果不使用inspect,直接使用puts输出数组,那么每个元素会占用一行输出2 irb(main):001:0> puts "Short sentence.Another.No more".split(/\./)3 Short sentence4 Another5 No more6 => nil7 irb(main):002:0> puts "Short sentence.Another.No more".split(/\./).inspect8 ["Short sentence", "Another", "No more"]9 => nil
数组迭代: 可以直接使用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 end10 111 212 313 414 => [nil, nil, nil, nil]15 irb(main):012:0> b16 => [nil, nil, nil, nil]17 # each永远返回原来的数组,没法改变原来的数组18 irb(main):013:0> b = a.each do |element| end19 => [1, 2, 3, 4]20 irb(main):014:0> b21 => [1, 2, 3, 4]22 irb(main):015:0> b = a.each do |element| element*3 end23 => [1, 2, 3, 4]24 irb(main):016:0> b25 => [1, 2, 3, 4]26 # each一般用来进行相关操作27 irb(main):018:0> a.each do |element| puts element end28 129 230 331 432 => [1, 2, 3, 4]
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 => nil10 # 判断是否包含某元素11 irb(main):022:0> a.include?(3)12 => true13 # 返回第一个元素14 irb(main):023:0> a.first15 => 116 # 返回最后一个元素17 irb(main):024:0> a.last18 => 419 # 返回开始的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.reverse26 => [4, 3, 2, 1]27 # 原数组不会变28 irb(main):028:0> a29 => [1, 2, 3, 4]
散列表:
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 `'10 irb(main):031:0> dict2 = { 'cat'=>cat1}11 NameError: undefined local variable or method `cat1' for main:Object12 Did you mean? catch13 from (irb):3114 from /usr/bin/irb:11:in ` '15 irb(main):032:0> dict2 = {:cat=>'cat1'}16 => {:cat=>"cat1"}17 # length和size都可以得到长度18 irb(main):033:0> dict.length19 => 220 irb(main):034:0> dict.size21 => 222 irb(main):035:0> dict['cat']23 => "cat1"24 # 不支持.访问25 irb(main):036:0> dict.cat26 NoMethodError: undefined method `cat' for {"cat"=>"cat1", "dog"=>"dog1"}:Hash27 from (irb):3628 from /usr/bin/irb:11:in ` '
迭代散列表:
1 irb(main):037:0> dict.each do |key, value| puts "#{key} matches #{value}" end2 cat matches cat13 dog matches dog14 => { "cat"=>"cat1", "dog"=>"dog1"}
得到散列中的所有键和值:
1 irb(main):038:0> dict.keys.inspect2 => "[\"cat\", \"dog\"]"3 irb(main):039:0> dict.values.inspect4 => "[\"cat1\", \"dog1\"]"
删除散列中的元素:
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> dict11 => {}
添加元素:
可以直接每次使用中括号对属性进行赋值:
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> dict12 => { "cat"=>"cat1", "newDog"=>"dog2", "newCat"=>"cat2"}
散列表中可以嵌套散列表,我们可以通过多重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"
流程控制:
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> end10 => "orange"11 12 # 上例可以简化为13 irb(main):092:0> color = case fruit14 irb(main):093:1> when 'orange'15 irb(main):094:1> 'orange'16 irb(main):095:1> else 'unknown' end17 => "orange"18 irb(main):096:0> color19 => "orange"
while
until:与while相反,直到条件满足才跳出循环1 irb(main):097:0> x = 12 => 13 irb(main):098:0> until x > 994 irb(main):099:1> puts x 5 irb(main):100:1> x = x * 206 irb(main):101:1> end7 18 209 => nil