Created
September 21, 2010 15:58
-
-
Save chikamichi/589924 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Bar | |
def do_sth | |
puts "@proxy: #{@proxy.inspect}" | |
end | |
end | |
class Foo | |
include Bar | |
def initialize | |
@proxy = "john" | |
end | |
def as(proxy, &block) | |
puts self.inspect | |
# ---------------- | |
# works, yet cumbersome and not thread-safe: | |
bak = @proxy | |
# this value should trump the block's binding value for @a | |
@proxy = proxy | |
block.call | |
@proxy = bak | |
# ---------------- | |
# wip (fails): | |
o = self.class.new | |
o.instance_variable_set("@proxy", proxy) | |
o.instance_eval do | |
puts self.inspect | |
puts @proxy.inspect | |
block.call | |
end | |
end | |
end | |
Foo.new.instance_eval do | |
do_sth # => should output @proxy: john | |
puts | |
as('root') { do_sth } # => should output @proxy: root | |
puts | |
do_sth # => should output @proxy: john | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment