On Wed, Feb 22, 2006 at 07:57:58PM +0900, Emiel van de Laar wrote:
> Hi List,
> 
> In trying to get rubygems working with 1.9 I ran accross an error
> regarding the method "Module#define_method". Investigating further
> using the ri documentation I found out that the sample code
> (specifically the "send hack") doesn't work anymore with 1.9.
> 
> Can someone give me an indication of how things have changed. Perhaps
> then we can update the documentation as well.

class A
  def foo; "A#foo" end
  private :foo
end

a = A.new
begin
    a.foo
rescue NoMethodError
  $!.message                  # => "private method `foo' called for #<A:0xb7dc0e7c>"
end

RUBY_VERSION                                       # => "1.9.0"
a.funcall(:foo)                                    # => "A#foo"
a.instance_eval{ foo }                             # => "A#foo"
a.instance_eval{send(:foo)}                        # => "A#foo"
a.send(:foo)
__END__
# ~> -:17:in `send': private method `foo' called for #<A:0xb7dc0e7c> (NoMethodError)
# ~> 	from -:17

* if you know which private method you want to call, obj.instance_eval{ meth() } is fine
* if the method to be invoked is decided at runtime,
  
  obj.instance_eval{ send(meth_name) }

will work on both 1.8 and 1.9. obj.funcall(meth_name) is faster but 1.9-only, and
good old obj.send(meth_name) won't work in 1.9.


-- 
Mauricio Fernandez  -   http://eigenclass.org   -  non-trivial Ruby