1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Ruby】freezeを使用してオブジェクトを凍結させる。

Posted at

freezeについて

Rubyにはfreezeというインスタンスメソッドがあるらしい。

array = ["a", "b", "c"].freeze
array = array.map!{|content| content.succ}

p array
 can't modify frozen Array: ["a", "b", "c"] (FrozenError)

このようになる。

このfreezeメソッドを使用すると、オブジェクトを凍結させることができるらしい。
凍結 = 変更が効かなくなるらしい。

だから、array = ["a", "b", "c"].freezeこうすることで配列を凍結することができる。
そのため、次の行の処理array = array.map!{|content| content.succ}はエラーとなる。

参考

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?