https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112013-07-16T19:29:57ZRuby Issue Tracking SystemRuby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=405292013-07-16T19:29:57Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Whoops. I can't update the description. The implementation should be:</p>
<p>OpenStruct.new(hash).instance_eval { binding }</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=405372013-07-17T04:09:19ZAnonymous
<ul></ul><p>Personally I think hash keys should be local variables in the binding, not method calls against self.</p>
<p>It's hard to express this in Ruby, but this can easily be done from the C side.</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=405382013-07-17T04:10:33ZAnonymous
<ul></ul><p>PS: I'm neutral towards this feature. I've got no strong feelings that it should or shouldn't be part of Ruby.</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=405542013-07-18T06:26:34Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>I don't mind on it being a local var in the binding since it should work either way, just an implementation detail I'd say... Maybe other Ruby implementations might prefer to use them as methods?</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=410192013-08-09T18:56:09Zko1 (Koichi Sasada)
<ul><li><strong>Assignee</strong> set to <i>ko1 (Koichi Sasada)</i></li></ul><p>What do you think about [Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Binding#local_variable_get, set, defined? (Closed)" href="https://redmine.ruby-lang.org/issues/8761">#8761</a>]?</p>
<p>Usage:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">get_empty_binding</span>
<span class="nb">binding</span>
<span class="k">end</span>
<span class="o">...</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">get_empty_binding</span>
<span class="nb">hash</span><span class="p">.</span><span class="nf">each</span><span class="p">{</span><span class="o">|</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="o">|</span>
<span class="n">b</span><span class="p">.</span><span class="nf">local_variable_set</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
<span class="p">}</span>
<span class="c1"># use b</span>
</code></pre>
<p>I think that <code>Binding#local_variable_set()</code> can be extended to accept one hash parameter (pairs of local variable name and value).</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">b</span> <span class="o">=</span> <span class="n">get_empty_binding</span>
<span class="n">b</span><span class="p">.</span><span class="nf">local_variable_set</span><span class="p">(</span><span class="nb">hash</span><span class="p">)</span>
<span class="n">b</span><span class="p">.</span><span class="nf">local_variable_set</span><span class="p">(</span><span class="ss">a: </span><span class="mi">1</span><span class="p">,</span> <span class="ss">b: </span><span class="mi">2</span><span class="p">)</span>
</code></pre> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=410392013-08-09T22:08:56Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>I don't quite understand how that would help me, Koichi.</p>
<p>How could I use <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Binding#local_variable_get, set, defined? (Closed)" href="https://redmine.ruby-lang.org/issues/8761">#8761</a> to get the same result as the example in the description?</p>
<p>ERB.new(IO.read 'template.erb').result Binding.from_hash(template_local: 'example')</p>
<p>My current alternative is:</p>
<p>ERB.new(IO.read 'template.erb').result OpenStruct.new(template_local: 'example'){ binding }</p>
<p>How would <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Binding#local_variable_get, set, defined? (Closed)" href="https://redmine.ruby-lang.org/issues/8761">#8761</a> make it easier for me?</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=410402013-08-09T22:10:09Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>In other words, I want an easier way to convert a hash to a binding. I first thought about Hash#to_binding but it didn't feel right to me...</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=410462013-08-09T23:53:33Zko1 (Koichi Sasada)
<ul></ul><p>(2013/08/09 22:10), rosenfeld (Rodrigo Rosenfeld Rosas) wrote:</p>
<blockquote>
<p>In other words, I want an easier way to convert a hash to a binding. I first thought about <code>Hash#to_binding</code> but it didn't feel right to me...</p>
</blockquote>
<p>Ok. Maybe I misunderstood your proposal.</p>
<p>What is conversion from <code>Hash</code> to <code>Binding</code>?<br>
I think it is <code>Binding</code> has a local variables which specified pairs in Hash.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">hash</span> <span class="o">=</span> <span class="p">{</span><span class="ss">a: </span><span class="mi">1</span><span class="p">,</span> <span class="ss">b: </span><span class="mi">2</span><span class="p">}</span>
<span class="n">b</span> <span class="o">=</span> <span class="no">Binding</span><span class="p">.</span><span class="nf">to_hash</span><span class="p">(</span><span class="nb">hash</span><span class="p">)</span>
<span class="nb">eval</span><span class="p">(</span><span class="s2">"p [a, b]"</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="c1">#=> [1, 2]</span>
</code></pre>
<p>Could you explain what do you want?</p>
<p>--<br>
// SASADA Koichi at atdot dot net</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=410642013-08-10T19:29:15Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>(13/08/09 23:34), SASADA Koichi wrote:</p>
<blockquote>
<p>What is conversion from <code>Hash</code> to <code>Binding</code>?<br>
I think it is Binding has a local variables which specified pairs in <code>Hash</code>.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">hash</span> <span class="o">=</span> <span class="p">{</span><span class="ss">a: </span><span class="mi">1</span><span class="p">,</span> <span class="ss">b: </span><span class="mi">2</span><span class="p">}</span>
<span class="n">b</span> <span class="o">=</span> <span class="no">Binding</span><span class="p">.</span><span class="nf">to_hash</span><span class="p">(</span><span class="nb">hash</span><span class="p">)</span>
</code></pre>
</blockquote>
<p><code>Binding.from_hash</code> ?</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=410942013-08-12T00:55:04Zko1 (Koichi Sasada)
<ul></ul><p>nobu (Nobuyoshi Nakada) wrote:</p>
<blockquote>
<p><code>Binding.from_hash</code> ?</p>
</blockquote>
<p>Yes.</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=411082013-08-12T21:18:25Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Koichi-san, that's correct:</p>
<p>eval 'p a, b', Binding.from_hash(a: 1, b: 2) #=> 1, 2</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=411132013-08-12T23:26:31Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>I didn't notice I replied only to Koichi Sasada when replying to the ruby-core list. Is it possible to set it up so that the reply-to field is set to ruby-core?</p>
<p>Here is some discussion from us from those e-mails so that everyone could have access to it:</p>
<blockquote>
<p>(2013/08/10 20:47), Rodrigo Rosenfeld Rosas wrote:</p>
<blockquote>
<p>I'm not sure how else to explain it</p>
<p>The only API example I know that requires a binding is the ERB one</p>
<p>It's designed to be used this way:</p>
<p>a = 1<br>
b = 2<br>
erb.result binding # both a and b are available inside the template as<br>
well as other methods and variables, like erb<br>
Koichi wrote:</p>
</blockquote>
<p>Please try:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">require</span> <span class="s1">'erb'</span>
<span class="n">bind</span> <span class="o">=</span> <span class="nb">binding</span>
<span class="n">bind</span><span class="p">.</span><span class="nf">local_variable_set</span><span class="p">(</span><span class="ss">:a</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">bind</span><span class="p">.</span><span class="nf">local_variable_set</span><span class="p">(</span><span class="ss">:b</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="nb">puts</span> <span class="no">ERB</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s2">"<%= a %> and <%= b %>"</span><span class="p">).</span><span class="nf">result</span><span class="p">(</span><span class="n">bind</span><span class="p">)</span>
<span class="c1">#=> 1 and 2</span>
</code></pre>
</blockquote>
<p>That works, but it is too much trouble for a simple requirements and<br>
besides that more methods and variables may leak when using the current<br>
binding.</p>
<p>That's why people will often use the "<code>OpenStruct.new(hash){binding}</code>"<br>
trick, since it's much shorter, but still a hack in my opinion.</p>
<p>This is often used in automation tools like Chef or Puppet where the<br>
recipe settings (stored as a hash, usually) should be available for some<br>
templates.</p>
<p>Suppose you have the NewRelic settings under <code>settings[:new_relic]</code> hash<br>
(eg.: <code>{ application_name: 'My App', enable_rum: true }</code>)</p>
<p>In such cases, when generating the newrelic.yml from a newrelic.yml.erb<br>
template, those tools would process it as:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">File</span><span class="p">.</span><span class="nf">write</span> <span class="s1">'newrelic/location/newrelic.yml'</span><span class="p">,</span> <span class="no">ERB</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="no">File</span><span class="p">.</span><span class="nf">read</span> <span class="s1">'path/to/newrelic.yml.erb'</span><span class="p">).</span><span class="nf">result</span> <span class="no">OpenStruct</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="n">settings</span><span class="p">[</span><span class="ss">:new_relic</span><span class="p">]){</span><span class="nb">binding</span><span class="p">}</span>
</code></pre>
<p>That's why I've created two tickets based on this common requirement. On<br>
this specific one I'm suggesting a <code>Binding.from_hash(hash)</code> to make it<br>
easier to get a binding from a hash for usage in such API's. The other<br>
one suggested ERB to accept also a hash, instead of a binding for <code>#result</code>.</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=411142013-08-12T23:27:11Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Koichi then replied with:</p>
<p>I'm not sure what methods and variables are leaks.</p>
<p>For example, only "make_binding" mathod is leaked.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">make_binding</span><span class="p">(</span><span class="nb">hash</span><span class="p">)</span>
<span class="n">__b</span> <span class="o">=</span> <span class="nb">binding</span>
<span class="nb">hash</span><span class="p">.</span><span class="nf">each</span><span class="p">{</span><span class="o">|</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="o">|</span>
<span class="n">__b</span><span class="p">.</span><span class="nf">local_variable_set</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
<span class="p">}</span>
<span class="n">__b</span>
<span class="k">end</span>
<span class="n">created_binding</span> <span class="o">=</span> <span class="n">make_binding</span><span class="p">(</span><span class="ss">a: </span><span class="mi">1</span><span class="p">,</span> <span class="ss">b: </span><span class="mi">2</span><span class="p">)</span>
<span class="o">...</span>
</code></pre> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=411152013-08-12T23:31:13Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>For that tiny script, this is true, Koichi, but usually we get a binding from some class, so all methods would be available as well as other intermediary local variables in the method calling <code>erb#results</code>.</p>
<p>Like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">A</span>
<span class="k">def</span> <span class="nf">a</span>
<span class="mi">1</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">b</span>
<span class="nb">eval</span> <span class="s1">'p a'</span><span class="p">,</span> <span class="nb">binding</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="no">A</span><span class="p">.</span><span class="nf">new</span><span class="p">.</span><span class="nf">b</span> <span class="c1"># a has leaked</span>
</code></pre> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=473892014-06-26T12:40:40Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul><li><strong>File</strong> <a href="/attachments/4499">feature-8643.pdf</a> <a class="icon-only icon-download" title="Download" href="/attachments/download/4499/feature-8643.pdf">feature-8643.pdf</a> added</li></ul><p>Add slide for proposal</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=598722016-08-02T02:41:14Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>Rodrigo Rosenfeld Rosas wrote:</p>
<blockquote>
<p>The other one suggested ERB to accept also a hash, instead of a binding for <code>#result</code>.</p>
</blockquote>
<p>It feels better to me.</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=598732016-08-02T02:42:26Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/59873/diff?detail_id=42049">diff</a>)</li></ul> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=599052016-08-03T19:38:01Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Nobuyoshi Nakada wrote:</p>
<blockquote>
<p>Rodrigo Rosenfeld Rosas wrote:</p>
<blockquote>
<p>The other one suggested ERB to accept also a hash, instead of a binding for <code>#result</code>.</p>
</blockquote>
<p>It feels better to me.</p>
</blockquote>
<p>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 <code>Binding.from_hash</code> I agree with you that passing a hash to ERB constructor feels better.</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=599062016-08-03T19:41:05Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>It seems this feature is not related at all to <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: test_aes_gcm_wrong_tag(OpenSSL::TestCipher) fails randomly (Closed)" href="https://redmine.ruby-lang.org/issues/8439">#8439</a>, could you please review it and remove the related feature? Please relate this ticket to <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Add a new method to ERB to allow assigning the local variables from a hash (Closed)" href="https://redmine.ruby-lang.org/issues/8631">#8631</a> instead.</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=600272016-08-10T02:54:25Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul><li><strong>Related to</strong> deleted (<i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/8430">Feature #8430</a>: Rational number literal</i>)</li></ul> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=600292016-08-10T02:54:38Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/8631">Feature #8631</a>: Add a new method to ERB to allow assigning the local variables from a hash</i> added</li></ul> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=600312016-08-10T02:57:29Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>Ticket links changed.</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=600482016-08-10T11:45:39Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Thanks!</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=628812017-02-06T03:29:34Zko1 (Koichi Sasada)
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>Can I close this issue?</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=628882017-02-06T12:21:46Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>If <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Add a new method to ERB to allow assigning the local variables from a hash (Closed)" href="https://redmine.ruby-lang.org/issues/8631">#8631</a> was accepted, I think it could be okay to close this one as this is the only use-case for this I've needed so far. But before <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Add a new method to ERB to allow assigning the local variables from a hash (Closed)" href="https://redmine.ruby-lang.org/issues/8631">#8631</a> is accepted, this one would be also a good alternative...</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=650932017-05-25T15:47:57Zk0kubun (Takashi Kokubun)takashikkbn@gmail.com
<ul><li><strong>Status</strong> changed from <i>Feedback</i> to <i>Rejected</i></li></ul><p>Since [Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Add a new method to ERB to allow assigning the local variables from a hash (Closed)" href="https://redmine.ruby-lang.org/issues/8631">#8631</a>] is accepted, closing this ticket.</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=650962017-05-25T18:30:48Zrosenfeld (Rodrigo Rosenfeld Rosas)rr.rosas@gmail.com
<ul></ul><p>Yes, it makes sense. Thanks a lot! :)</p> Ruby master - Feature #8643: Add Binding.from_hashhttps://redmine.ruby-lang.org/issues/8643?journal_id=955132021-12-23T23:40:08Zhsbt (Hiroshi SHIBATA)hsbt@ruby-lang.org
<ul><li><strong>Project</strong> changed from <i>14</i> to <i>Ruby master</i></li></ul>