Home

Awesome

Kernel#instance_variables_from [version] [ci]

Automatically turn bindings, hashes or arrays into instance variables. Instead of:

def initialize(a, b)
  @a = a
  @b = b
end

You can write:

def initialize(a, b)
  instance_variables_from binding # will assign @a and @b
end

It also works with hashes:

params = { c: 3, d: 4 }
instance_variables_from params # will assign @c and @d

It also works with arrays:

list = %w[instance variable]
instance_variables_from list # will assign @_0 and @_1

When you pass additional arguments, they will be interpreted as whitelist:

params = { c: 3, d: 4 }
instance_variables_from params, :c # will only assign @c

Setup

Add to your Gemfile:

gem 'instance_variables_from'

MIT License

Copyright (C) 2010-2016 Jan Lelis https://janlelis.com. Released under the MIT license.