Kernel#instancevariablesfrom [![[version]](https://badge.fury.io/rb/instancevariablesfrom.svg)](http://badge.fury.io/rb/instancevariablesfrom) [![[ci]](https://github.com/janlelis/instancevariablesfrom/workflows/Test/badge.svg)](https://github.com/janlelis/instancevariablesfrom/actions?query=workflow%3ATest)

January 2, 2021 ยท View on GitHub

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.