himitsu girlsの日記

 | 

2006-09-17

[] 11章 Arrayクラス

メソッドいろいろ。すんげーたくさん。

a = [nil, 1, nil, 2]
a.compact!
p a   #=> [1, 2] 配列中からnilを取り除く

a = [1, 2, 2, 3, 4]
a.delete(2)
p a   #=> [1,3,4]  配列中の2を削除

a = [1, 2, 3, 4]
a.delete_at(2)
p a   #=> [1,2,4]  配列のインデックス2を削除

a = [1, 2, 3, 4, 5]
a.delete_if { |i| i > 2 }
p a   #=> [1, 2]   ブロックを実行した結果が真なら削除

a = [1, 2, 3, 4, 5]
a.slice!(1, 2)   # 指定した部分を取り除く
p a   #=> [1, 4, 5]

a = [1, 2, 3, 4, 5, 2, 3, 4]
a.uniq!
p a   #=> [1, 2, 3, 4, 5]  重複要素を削除

a = [1, 2, 3, 4, 5]
a.shift
p a  #=> [2, 3, 4, 5]
a.pop
p a  #=> [2, 3, 4]
a.unshift(10)
p a  #=> [10, 2, 3, 4]

a = [2, 4, 6, 8]
b = a.map { |i| i * 3 }
p b  #=> [6, 12, 18, 24]

p [1, 2, 3, 4, 5].fill(0)        #=> [0, 0, 0, 0, 0]
p [1, 2, 3, 4, 5].fill(0, 3)     #=> [1, 2, 3, 0, 0]  fill(val, start)
p [1, 2, 3, 4, 5].fill(0, 1, 2)  #=> [1, 0, 0, 4, 5]  fill(val, start, length)
p [1, 2, 3, 4, 5].fill(0, 1..2)  #=> [1, 0, 0, 4, 5]

a = []
p a.fill(0, 3) { |i| i }      #=> [0, 1, 2]
p a.fill(4, 1) { |i| i }      #=> [0, 1, 2, nil, 4]

p a.fill("var")               #=> ["var", "var", "var", "var", "var"]
a.each { |i| p i.object_id }  #=> 134703780, 134703780, ...
a[0].upcase!
p a                           #=> ["VAR", "VAR", "VAR", "VAR", "VAR"]

p a.fill {"foo"}              #=> ["foo", "foo", "foo", "foo", "foo"]
a.each { |i| p i.object_id }  #=> 134703710, 134703640, ...
a[0].upcase!
p a                           #=> ["FOO", "foo", "foo", "foo", "foo"]

a = [1, [2, 3, [4]], 5]
a.flatten!
p a  #=> [1, 2, 3, 4, 5]  配列の入れ子を展開

a = [5, -1, 2, 2.5]
a.sort!
p a  #=> [-1, 2, 2.5, 5]

MarvMarv2012/10/19 23:15I was really confused, and this answreed all my questions.

vznpvovznpvo2012/10/20 07:46ZiDNp0 <a href="http://milcabwdldlx.com/">milcabwdldlx</a>

nycqnrnycqnr2012/10/21 06:24wlv3Yc , [url=http://xexphadgxllc.com/]xexphadgxllc[/url], [link=http://ebqlvephvzum.com/]ebqlvephvzum[/link], http://mlxwiabygpyn.com/

nycqnrnycqnr2012/10/21 06:24wlv3Yc , [url=http://xexphadgxllc.com/]xexphadgxllc[/url], [link=http://ebqlvephvzum.com/]ebqlvephvzum[/link], http://mlxwiabygpyn.com/

dkzcdeidkzcdei2012/10/21 09:309BdvOP <a href="http://fhambcxshxiv.com/">fhambcxshxiv</a>

btljyrduabtljyrdua2012/10/22 15:50KPstUj , [url=http://htwbmrsqvvpj.com/]htwbmrsqvvpj[/url], [link=http://wsdnlfyvikvw.com/]wsdnlfyvikvw[/link], http://ptldgpumnrel.com/

トラックバック - http://rubyist.g.hatena.ne.jp/himitsu_girls/20060917
 |