Temporary Execution Context With Included Modules
Join the DZone community and get the full member experience.
Join For FreeI needed to extend "self" with a module but only wanted the included methods to stick around "temporarily". After a lot of futile attempts with binding, callcc, eval, etc... I found it was as simple as "self.dup.instance_eval { ... } ":
def foo
'bar'
end
module M
def foo
'baz'
end
end
puts "should be 'bar': #{foo}"
self.dup.instance_eval do
extend(M)
puts "should be 'baz': #{foo}"
end
puts "should be 'bar': #{foo}"
Execution (computing)
Opinions expressed by DZone contributors are their own.
Comments