[ruby-core:76689] [CommonRuby Feature#8643] Add Binding.from_hash
From:
rr.rosas@...
Date:
2016-08-03 19:38:02 UTC
List:
ruby-core #76689
Issue #8643 has been updated by Rodrigo Rosenfeld Rosas.
Nobuyoshi Nakada wrote:
> Rodrigo Rosenfeld Rosas wrote:
> > The other one suggested ERB to accept also a hash, instead of a binding for `#result`.
>
> It feels better to me.
Either one is fine to me as long as I can easily pass locals to ERB from a hash using a proper API :) Since this is the only use case I have in mind for `Binding.from_hash` I agree with you that passing a hash to ERB constructor feels better.
----------------------------------------
Feature #8643: Add Binding.from_hash
https://bugs.ruby-lang.org/issues/8643#change-59905
* Author: Rodrigo Rosenfeld Rosas
* Status: Open
* Priority: Normal
* Assignee: Koichi Sasada
----------------------------------------
`Binding.from_hash` would work like:
```ruby
class Binding
def self.from_hash(hash)
OpenStruct.new(hash){ binding }
end
end
```
It would simplify things like:
`ERB.new(IO.read 'template.erb').result Binding.from_hash(template_local: 'example')`
Or if you need to `eval` some code in another process (JRuby, for instance) and need to pass some arguments to the `eval` code in a hash form.
I didn't want to pollute `Hash` by adding `Hash#to_binding`. I believe `Binding.from_hash` is more appropriate.
---Files--------------------------------
feature-8643.pdf (19 KB)
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>