Sha256: ad8255099e9aded9807b51267f41ba75a6d6c093da48b40dac13af6091cfadb7

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

# Kernel#instance_variables_from [![[version]](https://badge.fury.io/rb/instance_variables_from.svg)](http://badge.fury.io/rb/instance_variables_from)  [![[travis]](https://travis-ci.org/janlelis/instance_variables_from.png)](https://travis-ci.org/janlelis/instance_variables_from)

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

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

You can write:

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

It also works with hashes:

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

It also works with arrays:

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

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

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

## Setup

Add to your `Gemfile`:

```ruby
gem 'instance_variables_from'
```


## MIT License

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
instance_variables_from-1.0.0 README.md