P
Phrogz
Is there a way to call a proc while passing in the binding/scope for
'self'? I found an un-responded-to question on this (ruby-talk:12794)
in 2001 which implies that, at least then, there was no way to do it.
What I'm doing:
class Foo
@@handlers = {}
def self.handle_type( type, &proc )
@@handlers[ type ] = proc
end
attr_reader :name
def initialize( name )
@name = name
end
def do_thing( type )
@@handlers[ type ].call( self )
end
end
Foo.handle_type( :showname ){ |me|
puts "My name is '#{me.name}'"
}
f = Foo.new( 'f' )
f.do_thing( :showname )
#=> My name is 'f'
What I'd like to do instead:
Foo.handle_type( :showname ){
puts "My name is '#{self.name}'"
}
'self'? I found an un-responded-to question on this (ruby-talk:12794)
in 2001 which implies that, at least then, there was no way to do it.
What I'm doing:
class Foo
@@handlers = {}
def self.handle_type( type, &proc )
@@handlers[ type ] = proc
end
attr_reader :name
def initialize( name )
@name = name
end
def do_thing( type )
@@handlers[ type ].call( self )
end
end
Foo.handle_type( :showname ){ |me|
puts "My name is '#{me.name}'"
}
f = Foo.new( 'f' )
f.do_thing( :showname )
#=> My name is 'f'
What I'd like to do instead:
Foo.handle_type( :showname ){
puts "My name is '#{self.name}'"
}