Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
appending an array to a frozen array
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Robert Klemme, post: 4634967"] Since you are using an instance variable you control how it is accessed. So, yes. If you want to know whether there is a hard way to prevent reassignment to instance variables, the only safe way I know off the top of my head is to freeze the owner: irb(main):001:0> o=Object.new => #<Object:0x10171ed8> irb(main):002:0> o.instance_variable_set "@foo", 1 => 1 irb(main):003:0> o => #<Object:0x10171ed8 @foo=1> irb(main):004:0> o.freeze => #<Object:0x10171ed8 @foo=1> irb(main):005:0> o.instance_variable_set "@foo", 2 RuntimeError: can't modify frozen object from (irb):5:in `instance_variable_set' from (irb):5 from /opt/bin/irb19:12:in `<main>' irb(main):006:0> Kind regards robert [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
appending an array to a frozen array
Top