Project

iso-init

0.0
No commit activity in last 3 years
No release in over 3 years
Attempts to isolate initialize arguments
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 1.3
>= 0
>= 0
 Project Readme

Iso::Init

This is a gem to help Isolate object initialization.

What's wrong with the following?

SomeKlass = Class.new(Struct.new(options))
options = {foo: bar}
object = SomeKlass.new(options)
object.options
# => { :foo => :bar }
options[:baz] = bingo # DANGER, WILL ROBINSON
object.options
# => { :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(options)) { include IsoInit }
options = {foo: bar}
object = SomeKlass.new(options)
object.options
# => { :foo => :bar }
options[:baz] = bingo # DANGER, WILL ROBINSON ???
object.options
# => { :foo => :bar }
# Sweet! It didn't change!
object.options.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

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Dual-licensed under MIT and WTFPL.