Skip to content

Instantly share code, notes, and snippets.

@chikamichi
Created September 21, 2010 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chikamichi/589924 to your computer and use it in GitHub Desktop.
Save chikamichi/589924 to your computer and use it in GitHub Desktop.
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