Forum: Ruby erb and scope

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B7f73e9f9d35be6a44b15f603f2910d4?d=identicon&s=25 wybo (Guest)
on 2005-12-11 12:11
(Received via mailing list)
Why does this not work (bar->undefined) and what's a better way to do
it?:

require 'erb'

FOO = 'Hello <%= bar %>'
class Foo
  def doit
    bar = 'World!'
    puts ERB.new(FOO).result
  end
end

Foo.new.doit

I need to use $bar instead of bar to get it going...
2d532341317628fbb2cb22ec427a1d62?d=identicon&s=25 langstefan (Guest)
on 2005-12-11 12:28
(Received via mailing list)
On Sunday 11 December 2005 12:08, Wybo Dekker wrote:
> Why does this not work (bar->undefined) and what's a better way to
> do it?:
>
> require 'erb'
>
> FOO = 'Hello <%= bar %>'
> class Foo
>   def doit
>     bar = 'World!'
>     puts ERB.new(FOO).result

replace above line with:

      puts ERB.new(FOO).result(binding)

>   end
> end
>
> Foo.new.doit

Regards,
  Stefan
C3274d0d109830f7f5121a7d8f7ead98?d=identicon&s=25 malte__ (Guest)
on 2005-12-11 13:33
(Received via mailing list)
Wybo Dekker:
[erb]
> I need to use $bar instead of bar to get it going...

@bar will do it, too, and that's maybe the best way to do it.

Malte
B7f73e9f9d35be6a44b15f603f2910d4?d=identicon&s=25 wybo (Guest)
on 2005-12-11 13:37
(Received via mailing list)
On Sun, 11 Dec 2005, Stefan Lang wrote:

> >     puts ERB.new(FOO).result
>
> replace above line with:
>
>       puts ERB.new(FOO).result(binding)

That helps (although I don't understand it yet, but I'll find out),
thanks!
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 fxn (Guest)
on 2005-12-11 15:17
(Received via mailing list)
On Dec 11, 2005, at 13:32, Malte Milatz wrote:

> Wybo Dekker:
> [erb]
>> I need to use $bar instead of bar to get it going...
>
> @bar will do it, too, and that's maybe the best way to do it.

[Other poster suggests passing binding.]

I have always wondered why erb does not provide in addition the
possibility to pass a regular hash. In general I don't want to design
my objects or scopes around a template. Is there a rationale behind
that?

-- fxn
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-11 17:08
(Received via mailing list)
On Dec 11, 2005, at 6:36 AM, Wybo Dekker wrote:

> On Sun, 11 Dec 2005, Stefan Lang wrote:
>
>>>     puts ERB.new(FOO).result
>>
>> replace above line with:
>>
>>       puts ERB.new(FOO).result(binding)
>
> That helps (although I don't understand it yet, but I'll find out),
> thanks!

ERb templates are resolved in the scope of some "binding".  They have
access to the variables in that binding.  There is a private method
on Object (universally available) called binding(), which just
returns a Binding object for the current scope.  By handing that to
ERb, you can control what it can access.

Hope that helps.

James Edward Gray II
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2005-12-11 17:27
(Received via mailing list)
On Dec 11, 2005, at 7:49 AM, Xavier Noria wrote:

> I have always wondered why erb does not provide in addition the
> possibility to pass a regular hash. In general I don't want to
> design my objects or scopes around a template. Is there a rationale
> behind that?

I imagine the reason is because ERb uses eval() under the hood and
the tool eval() gives us is Binding.  We should be able to use that
to do pretty much what you want though:

 >> require "erb"
=> true
 >> class Template
 >>   def initialize( variables )
 >>     variables.each { |name, value| instance_variable_set("@#
{name}", value) }
 >>   end
 >>
?>   def resolve( template )
 >>     ERB.new(template).result(binding)
 >>   end
 >> end
=> nil
 >> t = Template.new(:one_var => "One", :two_var => "Two")
=> #<Template:0x3253dc @two_var="Two", @one_var="One">
 >> t.resolve("This is ERb using <%= @one_var %> and <%= @two_var %>.")
=> "This is ERb using One and Two."

James Edward Gray II
This topic is locked and can not be replied to.