読者です 読者をやめる 読者になる 読者になる

ログってなんぼ

日々のメモです

ruby見習い:dupとcloneと私

ruby

ruby見習い:dupとcloneと私

先日仕事変わったのですが、rubyの職場なので土日も勉強です。

ということで誰の役に立つかわかりませんがメモ置いときます。 Rubyは見習いLv1なので用語はおかしいと思います。

予習

aとbに"hoge"

irb(main):053:0> a = "hoge"
=> "hoge"
irb(main):054:0> b = "hoge"
=> "hoge"

値の比較

irb(main):055:0> a == b
=> true

objectの比較

irb(main):056:0> a.equal?(b)
=> false

2つは違うobject_id

irb(main):057:0> a.object_id
=> 70263748529040
irb(main):058:0> b.object_id
=> 70263748501140

b = a

irb(main):059:0> b = a
=> "hoge"

値は当然同じ

irb(main):060:0> a == b
=> true

object比較もtrueとなった

irb(main):061:0> a.equal?(b)
=> true

object_idが同一になっていることがわかる

irb(main):062:0> a.object_id
=> 70263748501140
irb(main):063:0> b.object_id
=> 70263748501140

bに再度"hoge"と入れてみる

irb(main):149:0> b = "hoge"
=> "hoge"
irb(main):150:0> b.object_id
=> 70263748592820
irb(main):151:0> a.object_id
=> 70263748690900
irb(main):152:0> a == b
=> true

また別物になった

b = a.dup

irb(main):064:0> b = a.dup
=> "hoge"
irb(main):065:0> a == b
=> true
irb(main):066:0> a.equal?(b)
=> false

irb(main):067:0> a.object_id
=> 70263748501140
irb(main):068:0> b.object_id
=> 70263748262820

bを変更してみる

irb(main):071:0> b = "fuga"
=> "fuga"
irb(main):072:0> a
=> "hoge"
irb(main):073:0> b
=> "fuga"

配列でやってみる dup編

irb(main):090:0* a = ["foo","bar","buzz"]
=> ["foo", "bar", "buzz"]
irb(main):091:0> b = a.dup
=> ["foo", "bar", "buzz"]

irb(main):092:0> a.object_id
=> 70263739858260
irb(main):093:0> b.object_id
=> 70263748828880

ある要素のみ別の文字列に上書きしてみる

irb(main):094:0> b[1] = "BAR"
=> "BAR"

irb(main):095:0> a
=> ["foo", "bar", "buzz"]
irb(main):096:0> b
=> ["foo", "BAR", "buzz"]

それぞれの要素にもobject_idがあるらしい

irb(main):097:0> a[1].object_id
=> 70263739858320
irb(main):098:0> b[1].object_id
=> 70263748757480

ほんまや・・

dupしてから変更のない要素は・・

irb(main):099:0> a[0].object_id
=> 70263739858340
irb(main):100:0> b[0].object_id
=> 70263739858340

(´・ω・)エッ?

文字を上書きするのではなくsubしてみる

irb(main):103:0> a[0].sub! /foo/,'FOO'
=> "FOO"

irb(main):104:0> a
=> ["FOO", "bar", "buzz"]
irb(main):105:0> b
=> ["FOO", "BAR", "buzz"]

(;゚д゚)ゴクリ…

freezeを試そう

irb(main):128:0> a = ["foo","bar","buzz"]
=> ["foo", "bar", "buzz"]
irb(main):129:0> b = a.dup
=> ["foo", "bar", "buzz"]

irb(main):130:0> a.freeze
=> ["foo", "bar", "buzz"]

新たな値を投入!

irb(main):131:0> a[0] = "FOO"
RuntimeError: can't modify frozen Array
from (irb):131:in `[]='
from (irb):131
from /Users/okisanjp/.rbenv/versions/2.0.0-p645/bin/irb:12:in `<main>'

普通に怒られました

bにも投入!

irb(main):132:0> b[0] = "FOO"
=> "FOO"

(´・ω・`)

clone

instance method Object#clone

コード書いてる時間とマニュアル見てる時間が同じくらいです・・・

cloneする

irb(main):133:0> b = a.clone
=> ["foo", "bar", "buzz"]
irb(main):134:0> a[0] = "FOO"
RuntimeError: can't modify frozen Array
from (irb):134:in `[]='
from (irb):134
from /Users/okisanjp/.rbenv/versions/2.0.0-p645/bin/irb:12:in `<main>'
irb(main):135:0> b[0] = "FOO"
RuntimeError: can't modify frozen Array
from (irb):135:in `[]='
from (irb):135
from /Users/okisanjp/.rbenv/versions/2.0.0-p645/bin/irb:12:in `<main>'

yay!yay!yay!

Ruby道への挑戦はまだまだ始まったばかり・・・