Hatena::Groupruby

mrのRuby学習帳 このページをアンテナに追加 RSSフィード

2009-09-27

arrayのdetectメソッド arrayのdetectメソッド - mrのRuby学習帳 を含むブックマーク

irb(main):001:0> a=[1,2,3]
=> [1, 2, 3]
irb(main):002:0> a.detect{|x| x==2}
=> 2
irb(main):003:0> a.detect{|x| x==3}
=> 3
irb(main):004:0> a.detect{|x| x==1}
=> 1
irb(main):005:0>

arrayのfind_allメソッド arrayのfind_allメソッド - mrのRuby学習帳 を含むブックマーク

irb(main):001:0> a=[1,2,3,4,5,6,7,8,9]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
irb(main):002:0> a.find_all{|x| x > 5}
=> [6, 7, 8, 9]
irb(main):003:0> a.find_all{|x| x.even?}
NoMethodError: undefined method `even?' for 1:Fixnum
        from (irb):3
        from (irb):3:in `find_all'
        from (irb):3:in `each'
        from (irb):3:in `find_all'
        from (irb):3
irb(main):004:0> a.find_all{|x| (x % 2).zero?}
=> [2, 4, 6, 8]

even?というメソッドはなかったか。

arrayのflattenメソッド arrayのflattenメソッド - mrのRuby学習帳 を含むブックマーク

irb(main):001:0> a=[1,2,[3,4],5,6]
=> [1, 2, [3, 4], 5, 6]
irb(main):002:0> a.flatten
=> [1, 2, 3, 4, 5, 6]

arrayのmapメソッド arrayのmapメソッド - mrのRuby学習帳 を含むブックマーク

irb(main):001:0> a=[1,2,[3,4],5,6]
=> [1, 2, [3, 4], 5, 6]
irb(main):002:0> a.flatten
=> [1, 2, 3, 4, 5, 6]

arrayのselectメソッド arrayのselectメソッド - mrのRuby学習帳 を含むブックマーク

irb(main):001:0> a=[1,2,3,4,5,6,7,8,9]
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
irb(main):002:0> a.select{|x| x < 5}
=> [1, 2, 3, 4]

arrayのsliceメソッド arrayのsliceメソッド - mrのRuby学習帳 を含むブックマーク

irb(main):001:0> a=[1,2,3,2,2,3,3,2,3,4,2,3,5,2,3,6,2,3,7,2,3,8,2,3,9,2,3]
=> [1, 2, 3, 2, 2, 3, 3, 2, 3, 4, 2, 3, 5, 2, 3, 6, 2, 3, 7, 2, 3, 8, 2, 3, 9, 2
, 3]
irb(main):002:0> a.slice(2)
=> 3
irb(main):003:0> a.slice(2..5)
=> [3, 2, 2, 3]
irb(main):004:0> a.slice(2,3)
=> [3, 2, 2]

arrayのuniqメソッド arrayのuniqメソッド - mrのRuby学習帳 を含むブックマーク

irb(main):001:0> a=[1,2,3,2,2,3,3,2,3,4,2,3,5,2,3]
=> [1, 2, 3, 2, 2, 3, 3, 2, 3, 4, 2, 3, 5, 2, 3]
irb(main):002:0> a.uniq
=> [1, 2, 3, 4, 5]

arrayのvalues_atメソッド arrayのvalues_atメソッド - mrのRuby学習帳 を含むブックマーク

irb(main):001:0> a=["zero","one","two","three"]
=> ["zero", "one", "two", "three"]
irb(main):002:0> a.values_at(1)
=> ["one"]
irb(main):003:0> a.values_at(2)
=> ["two"]
irb(main):004:0> a[1]
=> "one"
irb(main):005:0> a[2]
=> "two"

LusLus2012/08/14 16:40I'm imperssed. You've really raised the bar with that.

bshslwlbshslwl2012/08/15 00:09O7GbMH <a href="http://fjzodcjdvopg.com/">fjzodcjdvopg</a>

dstvaldstval2012/08/16 00:14g8AIOx , [url=http://hbvogwefwpnn.com/]hbvogwefwpnn[/url], [link=http://xjnvxkaptjcb.com/]xjnvxkaptjcb[/link], http://dvaqmsxzaqpd.com/

cprlyqpqfdkcprlyqpqfdk2012/08/16 14:01EUcr5b <a href="http://nzvyxlaayqmc.com/">nzvyxlaayqmc</a>

dtcvhardojcdtcvhardojc2012/08/17 02:42cO4l85 , [url=http://uyqknjpkdsdc.com/]uyqknjpkdsdc[/url], [link=http://gjswdilbadrg.com/]gjswdilbadrg[/link], http://uqfsqnxrmvuq.com/