X
Xavier Shay
I want the following eval to output "Foo called", without having a named
constant, since in leaks in to the calling scope:
m = Module.new
m.instance_eval do
def foo
puts "Foo called"
end
ParentSelf = self
class self::A
ParentSelf.foo # How do I do this without a constant?
end
end
a = m.const_get('A').new
puts ParentSelf # This should raise - constant should not exist
I have found given self and a global function, I can use the string
representation of the anon module to find it in ObjectSpace. Clearly
this is dodgy though:
def depend(instance, file)
q = self.to_s.split('::').first.to_s
found = nil
ObjectSpace.each_object(Module) do |m|
found = m if q == m.to_s
end
found.depend(file)
end
m = Module.new
m.instance_eval do
def depend(file)
puts "Depend called with file: #{file}"
end
class self::A
depend(self, 'afile.rb')
end
end
constant, since in leaks in to the calling scope:
m = Module.new
m.instance_eval do
def foo
puts "Foo called"
end
ParentSelf = self
class self::A
ParentSelf.foo # How do I do this without a constant?
end
end
a = m.const_get('A').new
puts ParentSelf # This should raise - constant should not exist
I have found given self and a global function, I can use the string
representation of the anon module to find it in ObjectSpace. Clearly
this is dodgy though:
def depend(instance, file)
q = self.to_s.split('::').first.to_s
found = nil
ObjectSpace.each_object(Module) do |m|
found = m if q == m.to_s
end
found.depend(file)
end
m = Module.new
m.instance_eval do
def depend(file)
puts "Depend called with file: #{file}"
end
class self::A
depend(self, 'afile.rb')
end
end