Forum: Ruby Can not find variable in the current scope!

Posted by David Unric (dunric)
on 2013-02-16 13:53
Hello,

when trying to add tracing output to an erb template I hit the following
issue when trying to find where variable ex_title comes from.

Tried to list all possible variables in the current scope, but there
simply does not exist:
<%= instance_variables.grep(/ex_title/) %>   # renders an empty array
<%= global_variables.grep(/ex_title/) %>    # renders an empty array
<%= local_variables.grep(/ex_title/) %>     # renders an empty array

<%= instance_variables.sort %>   # renders array with many elements
<%= global_variables.sort %>     # renders array with many elements
<%= local_variables.sort %>      # renders array with many elements

Whereas ex_title variable provably does exists:
<%= ex_title %>                  # renders value "Categories - Online
store"
<%= ex_title.class %>            # renders "String"
<%= ex_title.object_id %>        # renders "15825900"

Where else can be found if not in global, local or instance variables ?
Posted by Bartosz DziewoƄski (matmarex)
on 2013-02-16 14:29
(Received via mailing list)
It could be a method. Or erb is doing some crazy stuff.
Posted by Robert Klemme (robert_k78)
on 2013-02-16 15:49
(Received via mailing list)
On Sat, Feb 16, 2013 at 1:53 PM, David Unric <lists@ruby-forum.com> 
wrote:
> Hello,
>
> when trying to add tracing output to an erb template I hit the following
> issue when trying to find where variable ex_title comes from.

I cannot reproduce this issue

irb(main):001:0> e = ERB.new '(<%= x %>)'
=> #<ERB:0x802ae510 @safe_level=nil, @src="#coding:UTF-8\n_erbout =
''; _erbout.concat \"(\"; _erbout.concat(( x ).to_s); _erbout.concat
\")\"; _erbout.force_encoding(__ENCODING__)", @enc=#<Encoding:UTF-8>,
@filename=nil>
irb(main):002:0> x = 99
=> 99
irb(main):003:0> e.result binding
=> "(99)"

irb(main):004:0> e = ERB.new '(<%= x %>)(<%= local_variables.inspect
%>)(<%= local_variables.grep(/x/) %>)'
=> #<ERB:0x8029f8e4 @safe_level=nil, @src="#coding:UTF-8\n_erbout =
''; _erbout.concat \"(\"; _erbout.concat(( x ).to_s); _erbout.concat
\")(\"; _erbout.concat(( local_variables.inspect ).to_s);
_erbout.concat \")(\"; _erbout.concat(( local_variables.grep(/x/)
).to_s); _erbout.concat \")\"; _erbout.force_encoding(__ENCODING__)",
@enc=#<Encoding:UTF-8>, @filename=nil>
irb(main):005:0> e.result binding
=> "(99)([:_erbout, :x, :e, :_])([:x])"

Also outside IRB

$ ruby -r erb -e 'e=ERB.new("<%= x %>|<%= local_variables.sort.inspect
%>");x=99;puts e.result'
99|[:_erbout, :e, :x]
$ ruby -r erb -e 'e=ERB.new("<%= x %>|<%= local_variables.grep(/x/)
%>");x=99;puts e.result'
99|[:x]

> Tried to list all possible variables in the current scope, but there
> simply does not exists:
> <%= instance_variables.grep(/ex_title/) %>   # renders an empty array
> <%= global_variables..grep(/ex_title/) %>    # renders an empty array
> <%= local_variables..grep(/ex_title/) %>     # renders an empty array

Did you really execute this?  I get this

irb(main):008:0> f = ERB.new '<%= local_variables..grep(/x/) %>'
=> #<ERB:0x80253890 @safe_level=nil, @src="#coding:UTF-8\n_erbout =
''; _erbout.concat(( local_variables..grep(/x/) ).to_s);
_erbout.force_encoding(__ENCODING__)", @enc=#<Encoding:UTF-8>,
@filename=nil>
irb(main):009:0> f.result binding
NoMethodError: undefined method `grep' for main:Object
        from (erb):1
        from /usr/lib/ruby/1.9.1/erb.rb:838:in `eval'
        from /usr/lib/ruby/1.9.1/erb.rb:838:in `result'
        from (irb):9
        from /usr/bin/irb:12:in `<main>'

$ ruby -r erb -e 'e=ERB.new("<%= x %>|<%= local_variables..grep(/x/)
%>");x=99;puts e.result'
(erb):1:in `<main>': undefined method `grep' for main:Object 
(NoMethodError)
        from /usr/lib/ruby/1.9.1/erb.rb:838:in `eval'
        from /usr/lib/ruby/1.9.1/erb.rb:838:in `result'
        from -e:1:in `<main>'


If I omit the second dot I get

irb(main):010:0> f = ERB.new '<%= local_variables.grep(/x/) %>'
=> #<ERB:0x8022ed4c @safe_level=nil, @src="#coding:UTF-8\n_erbout =
''; _erbout.concat(( local_variables.grep(/x/) ).to_s);
_erbout.force_encoding(__ENCODING__)", @enc=#<Encoding:UTF-8>,
@filename=nil>
irb(main):011:0> f.result binding
=> "[:x]"


> Where else can be found if not in global, local or instance variables ?
You would have to show us how you evaluate those ERB templates.

Cheers

robert
Posted by Josh Cheek (josh-cheek)
on 2013-02-17 00:12
(Received via mailing list)
On Sat, Feb 16, 2013 at 6:53 AM, David Unric <lists@ruby-forum.com> 
wrote:

>
> Where else can be found if not in global, local or instance variables ?
>
> --
> Posted via http://www.ruby-forum.com/.
>
>
You have two dots, which would be a range. If that doesn't solve it, but
more probably, ex_title is a method

<%= methods.grep(/ex_title/) %>

-Josh
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.