Iso::Init
This is a gem to help Isolate object initialization.
What's wrong with the following?
SomeKlass = Class.new(Struct.new())
= {foo: }
object = SomeKlass.new()
object.
# => { :foo => :bar }
[:baz] = bingo # DANGER, WILL ROBINSON
object.
# => { :foo => :bar, :baz : bingo }
That's right. You passed an object by reference, so when that object changed, all references to it also changed, which was not what you intended to do.
IsoInit
fixes that.
Installation
Add this line to your application's Gemfile:
gem 'iso-init'
And then execute:
$ bundle
Or install it yourself as:
$ gem install iso-init
Usage
SomeKlass = Class.new(Struct.new()) { include IsoInit }
= {foo: }
object = SomeKlass.new()
object.
# => { :foo => :bar }
[:baz] = bingo # DANGER, WILL ROBINSON ???
object.
# => { :foo => :bar }
# Sweet! It didn't change!
object..frozen?
# => true
# ok. that's what I was hoping for. yay.
If an object is immutable, it won't be dup'd (e.g., true
, nil
, 1
, 3.1415926
), but if it is mutable, initialize
will be called with a frozen duplicate.
Sweeeeet.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request