rack-devel archive mirror (unofficial) https://groups.google.com/group/rack-devel
 help / color / mirror / code / Atom feed
* Issue using Ruby1.9 (instance_eval in /lib/rack/builder.rb)
@ 2009-09-15 11:50 Iñaki Baz Castillo
  2009-09-15 11:59 ` Magnus Holm
  0 siblings, 1 reply; 4+ messages in thread
From: Iñaki Baz Castillo @ 2009-09-15 11:50 UTC (permalink / raw)
  To: rack-devel


Using Ruby1.9 there is an issue related to instance_eval() method
which behaves different than in 1.8. Let me show this example:

--------myapp.rb------------------------
class MyRack

    MY_VAR = "helloooo"

    def self.create
        Rack::Builder.new do
            map "/" do
                puts "[1] MY_VAR = #{MY_VAR}"   <----- [1]  (line 20)
                run Proc.new { |env|
                    puts "[2] MY_VAR = #{MY_VAR}"   <----- [2]  (line 22)
                    [ 200, {"Content-Type" => "text/plain"}, ["Yeah"] ]
                }
            end
        end
    end

end


rack_core = MyRack.create

Rack::Handler::Thin.run rack_core
----------------------------------------


This works great in Ruby1.8.

However, in Ruby1.9 I get this error:


/myapp.rb:20:in `block (2 levels) in core': uninitialized constant
Rack::Builder::MY_VAR (NameError)
        from /usr/local/lib/ruby1.9/gems/1.9.1/gems/rack-1.0.0/lib/rack/builder.rb:29:in
`instance_eval'
        from /usr/local/lib/ruby1.9/gems/1.9.1/gems/rack-1.0.0/lib/rack/builder.rb:29:in
`initialize'
        from /usr/local/lib/ruby1.9/gems/1.9.1/gems/rack-1.0.0/lib/rack/builder.rb:46:in
`new'
        from /usr/local/lib/ruby1.9/gems/1.9.1/gems/rack-1.0.0/lib/rack/builder.rb:46:in
`map'
        from /root/svn_local_copies/OpenXDMS/trunk/lib/rack/core.rb:41:in
`block in core'
        from /usr/local/lib/ruby1.9/gems/1.9.1/gems/rack-1.0.0/lib/rack/builder.rb:29:in
`instance_eval'
        from /usr/local/lib/ruby1.9/gems/1.9.1/gems/rack-1.0.0/lib/rack/builder.rb:29:in
`initialize'



If I remove line 20, then it would in runtime in line 22 (when "run"
is invoked for a incoming request).


The only solution for it to work in 1.9 is to add ALL the Module/Class
namespace for ALL the modules/classes:

        Rack::Builder.new do
            map "/" do
                puts "[1] MY_VAR = #{::MyRack::MY_VAR}"   <----- [1]  (line 20)
                run Proc.new { |env|
                    puts "[2] MY_VAR = #{::MyRack::MY_VAR}"   <-----
[2]  (line 22)
                    [ 200, {"Content-Type" => "text/plain"}, ["Yeah"] ]
                }
            end
        end


It's VERY important to note that if I use "MyRack::MY_VAR" instead of
"::MyRack::MY_VAR" then it would *also* fail in Ruby1.9:

    `block (2 levels) in core': uninitialized constant
Rack::Builder::MyRack (NameError)


It seems that using instance_eval in Ruby1.9 a class/module is just
looked for into the class/module executing the instance_eval and
doesn't look on top of it.


Regards.


-- 
Iñaki Baz Castillo
<ibc@aliax.net>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Issue using Ruby1.9 (instance_eval in /lib/rack/builder.rb)
  2009-09-15 11:50 Issue using Ruby1.9 (instance_eval in /lib/rack/builder.rb) Iñaki Baz Castillo
@ 2009-09-15 11:59 ` Magnus Holm
  2009-09-15 12:46   ` Iñaki Baz Castillo
  0 siblings, 1 reply; 4+ messages in thread
From: Magnus Holm @ 2009-09-15 11:59 UTC (permalink / raw)
  To: rack-devel


Also see http://coderrr.wordpress.com/2009/06/02/fixing-constant-lookup-in-dsls-in-ruby-1-9/

//Magnus Holm



On Tue, Sep 15, 2009 at 13:50, Iñaki Baz Castillo <ibc@aliax.net> wrote:
>
> Using Ruby1.9 there is an issue related to instance_eval() method
> which behaves different than in 1.8. Let me show this example:
>
> --------myapp.rb------------------------
> class MyRack
>
>    MY_VAR = "helloooo"
>
>    def self.create
>        Rack::Builder.new do
>            map "/" do
>                puts "[1] MY_VAR = #{MY_VAR}"   <----- [1]  (line 20)
>                run Proc.new { |env|
>                    puts "[2] MY_VAR = #{MY_VAR}"   <----- [2]  (line 22)
>                    [ 200, {"Content-Type" => "text/plain"}, ["Yeah"] ]
>                }
>            end
>        end
>    end
>
> end
>
>
> rack_core = MyRack.create
>
> Rack::Handler::Thin.run rack_core
> ----------------------------------------
>
>
> This works great in Ruby1.8.
>
> However, in Ruby1.9 I get this error:
>
>
> /myapp.rb:20:in `block (2 levels) in core': uninitialized constant
> Rack::Builder::MY_VAR (NameError)
>        from /usr/local/lib/ruby1.9/gems/1.9.1/gems/rack-1.0.0/lib/rack/builder.rb:29:in
> `instance_eval'
>        from /usr/local/lib/ruby1.9/gems/1.9.1/gems/rack-1.0.0/lib/rack/builder.rb:29:in
> `initialize'
>        from /usr/local/lib/ruby1.9/gems/1.9.1/gems/rack-1.0.0/lib/rack/builder.rb:46:in
> `new'
>        from /usr/local/lib/ruby1.9/gems/1.9.1/gems/rack-1.0.0/lib/rack/builder.rb:46:in
> `map'
>        from /root/svn_local_copies/OpenXDMS/trunk/lib/rack/core.rb:41:in
> `block in core'
>        from /usr/local/lib/ruby1.9/gems/1.9.1/gems/rack-1.0.0/lib/rack/builder.rb:29:in
> `instance_eval'
>        from /usr/local/lib/ruby1.9/gems/1.9.1/gems/rack-1.0.0/lib/rack/builder.rb:29:in
> `initialize'
>
>
>
> If I remove line 20, then it would in runtime in line 22 (when "run"
> is invoked for a incoming request).
>
>
> The only solution for it to work in 1.9 is to add ALL the Module/Class
> namespace for ALL the modules/classes:
>
>        Rack::Builder.new do
>            map "/" do
>                puts "[1] MY_VAR = #{::MyRack::MY_VAR}"   <----- [1]  (line 20)
>                run Proc.new { |env|
>                    puts "[2] MY_VAR = #{::MyRack::MY_VAR}"   <-----
> [2]  (line 22)
>                    [ 200, {"Content-Type" => "text/plain"}, ["Yeah"] ]
>                }
>            end
>        end
>
>
> It's VERY important to note that if I use "MyRack::MY_VAR" instead of
> "::MyRack::MY_VAR" then it would *also* fail in Ruby1.9:
>
>    `block (2 levels) in core': uninitialized constant
> Rack::Builder::MyRack (NameError)
>
>
> It seems that using instance_eval in Ruby1.9 a class/module is just
> looked for into the class/module executing the instance_eval and
> doesn't look on top of it.
>
>
> Regards.
>
>
> --
> Iñaki Baz Castillo
> <ibc@aliax.net>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Issue using Ruby1.9 (instance_eval in /lib/rack/builder.rb)
  2009-09-15 11:59 ` Magnus Holm
@ 2009-09-15 12:46   ` Iñaki Baz Castillo
  2009-09-15 16:03     ` Yehuda Katz
  0 siblings, 1 reply; 4+ messages in thread
From: Iñaki Baz Castillo @ 2009-09-15 12:46 UTC (permalink / raw)
  To: rack-devel


2009/9/15 Magnus Holm <judofyr@gmail.com>:
>
> Also see http://coderrr.wordpress.com/2009/06/02/fixing-constant-lookup-in-dsls-in-ruby-1-9/

Really interesting. So it's a bug in Ruby1.9.
Is it known if it's being addressed by developers?

Thanks a lot.

-- 
Iñaki Baz Castillo
<ibc@aliax.net>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Issue using Ruby1.9 (instance_eval in /lib/rack/builder.rb)
  2009-09-15 12:46   ` Iñaki Baz Castillo
@ 2009-09-15 16:03     ` Yehuda Katz
  0 siblings, 0 replies; 4+ messages in thread
From: Yehuda Katz @ 2009-09-15 16:03 UTC (permalink / raw)
  To: rack-devel

[-- Attachment #1: Type: text/plain, Size: 657 bytes --]

I wouldn't call it a bug. It's more just a change in the semantics of Ruby.
Matz has said he's thinking about reverting this change at some point in the
future.
-- Yehuda

On Tue, Sep 15, 2009 at 8:46 AM, Iñaki Baz Castillo <ibc@aliax.net> wrote:

>
> 2009/9/15 Magnus Holm <judofyr@gmail.com>:
> >
> > Also see
> http://coderrr.wordpress.com/2009/06/02/fixing-constant-lookup-in-dsls-in-ruby-1-9/
>
> Really interesting. So it's a bug in Ruby1.9.
> Is it known if it's being addressed by developers?
>
> Thanks a lot.
>
> --
> Iñaki Baz Castillo
> <ibc@aliax.net>
>



-- 
Yehuda Katz
Developer | Engine Yard
(ph) 718.877.1325

[-- Attachment #2: Type: text/html, Size: 1277 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2009-09-15 16:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-09-15 11:50 Issue using Ruby1.9 (instance_eval in /lib/rack/builder.rb) Iñaki Baz Castillo
2009-09-15 11:59 ` Magnus Holm
2009-09-15 12:46   ` Iñaki Baz Castillo
2009-09-15 16:03     ` Yehuda Katz

Code repositories for project(s) associated with this inbox:

	https://80x24.org/mirrors/rack.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).