how do i execute a block for the toplevel from within a class method?
take a look:
class AnyOther
...
end
class AnyOld
def initialize
@goingup = AnyOther.new
eval "goneup = @goingup", TOPLEVEL_BINDING
end
end
this isn’t going to work b/c @goingup will be looked for in the toplevel too.
right? moreover i tried this and it executes without and error, but goneup
isn’t to be found.
a = AnyOld.new
puts goneup
reports goneup dosen’t exist. where did it go? mind you i’m running this from
with in an rhtml eruby script.
-transami
that’s odd. can get to it through eval but not otherwise? ugh, so is there no
way to dynamically define a toplevel local variable? that’s basically what
i’m trying to do.
-transami
···
On Sunday 05 January 2003 02:39 am, nobu.nokada@softhome.net wrote:
Hi,
At Sun, 5 Jan 2003 18:05:41 +0900, > > Tom Sawyer wrote:
a = AnyOld.new
puts goneup
reports goneup dosen’t exist. where did it go? mind you i’m running this
from with in an rhtml eruby script.
puts eval(“goneup”)
Hi,
a = AnyOld.new
puts goneup
reports goneup dosen’t exist. where did it go? mind you i’m running this
from with in an rhtml eruby script.
puts eval(“goneup”)
that’s odd. can get to it through eval but not otherwise? ugh, so is there no
way to dynamically define a toplevel local variable? that’s basically what
i’m trying to do.
No. Local variables (and constants now) are defined lexically.
You can add methods dynamically.
module Kernel
def metaclass_eval(&block)
(class << self; self; end).class_eval(&block)
end
end
class AnyOther
end
class AnyOld
def initialize
goingup = AnyOther.new
eval(“self”, TOPLEVEL_BINDING).metaclass_eval do
define_method(:goneup, proc {goingup})
end
end
end
a = AnyOld.new
puts goneup
···
At Sun, 5 Jan 2003 22:41:57 +0900, Tom Sawyer wrote:
–
Nobu Nakada
[snip]
so is there no way to dynamically define a toplevel local variable? that’s
basically what i’m trying to do.
[snip]
a toplevel local variable wouldn’t be very different from a global.
why not
eval ‘$goneup = 42’
-a
···
On Sun, 5 Jan 2003, Tom Sawyer wrote:
–
====================================
Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================
thanks Nobu! that will work. refined:
module Kernel
def toplevel
eval("self", TOPLEVEL_BINDING)
end
def metaclass_eval(&block)
(class << self; self; end).class_eval(&block)
end
def define_singleton(meth, &block)
metaclass_eval { define_method(meth, &block) }
end
end
i have added this to tomslib/rubylib (new release coming soon).
WARNING do not attempt:
class PANIC
def initialize
toplevel.define_singleton(:lockup) { self }
end
end
PANIC.new
lockup
i ran something like this via an apache/mod_ruby script and my machine locked
up. your milage may vary, but i doubt it will be good milage in any case
-transami
···
On Sunday 05 January 2003 08:01 am, nobu.nokada@softhome.net wrote:
No. Local variables (and constants now) are defined lexically.
You can add methods dynamically.
i recant my warning. this DOES NOT lockup up one’s system. something locked up
mine though related to this and trying to access an instance variable that
didn’t exist. ah well, the mysteries of coding.
-transami
···
On Sunday 05 January 2003 02:18 pm, Tom Sawyer wrote:
WARNING do not attempt:
class PANIC
def initialize
toplevel.define_singleton(:lockup) { self }
end
end
PANIC.new
lockup
i ran something like this via an apache/mod_ruby script and my machine
locked up. your milage may vary, but i doubt it will be good milage in any
case