Best way to dynamically create new instance methods

Sent: Monday, December 11, 2006 3:15 PM
To: ruby-talk ML
Subject: Re: best way to dynamically create new instance methods

>> def create_method(name)
>> class << self; self end.send(:define_method, name) { |*args|
args.first }
>> end

Ooh, look at Mr. Fancy Pants, using his fancy #send to get around
private methods.

:slight_smile:

Really, I'm writing this email not to praise or make fun, but to point
out that the above technique may not work when 1.9 rolls around.
(Assuming I remember correctly that 1.9 will/may change so that #send
honors the public/protected/private state of the method being invoked.)

路路路

From: James Edward Gray II [mailto:james@grayproductions.net]

Correct. In 1.9 you need to use funcall() in place of send().

James Edward Gray II

路路路

On Dec 11, 2006, at 4:19 PM, Gavin Kistner wrote:

From: James Edward Gray II [mailto:james@grayproductions.net]

Sent: Monday, December 11, 2006 3:15 PM
To: ruby-talk ML
Subject: Re: best way to dynamically create new instance methods

  def create_method(name)
    class << self; self end.send(:define_method, name) { |*args|

args.first }

  end

Ooh, look at Mr. Fancy Pants, using his fancy #send to get around
private methods.

:slight_smile:

Really, I'm writing this email not to praise or make fun, but to point
out that the above technique may not work when 1.9 rolls around.
(Assuming I remember correctly that 1.9 will/may change so that #send
honors the public/protected/private state of the method being invoked.)

Hi --

路路路

On Tue, 12 Dec 2006, James Edward Gray II wrote:

On Dec 11, 2006, at 4:19 PM, Gavin Kistner wrote:

From: James Edward Gray II [mailto:james@grayproductions.net]

Sent: Monday, December 11, 2006 3:15 PM
To: ruby-talk ML
Subject: Re: best way to dynamically create new instance methods

def create_method(name)
   class << self; self end.send(:define_method, name) { |*args|

args.first }

end

Ooh, look at Mr. Fancy Pants, using his fancy #send to get around
private methods.

:slight_smile:

Really, I'm writing this email not to praise or make fun, but to point
out that the above technique may not work when 1.9 rolls around.
(Assuming I remember correctly that 1.9 will/may change so that #send
honors the public/protected/private state of the method being invoked.)

Correct. In 1.9 you need to use funcall() in place of send().

And keep a little card in your pocket to remind you which does
which.... :slight_smile:

David

--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

I write such things this way nowadays for that reason:

class << self; self end.class_eval{ define_method(name){ |*args| args.first } }

路路路

On Tue, Dec 12, 2006 at 07:28:50AM +0900, James Edward Gray II wrote:

On Dec 11, 2006, at 4:19 PM, Gavin Kistner wrote:
>>>> def create_method(name)
>>>> class << self; self end.send(:define_method, name) { |*args|
>>args.first }
>>>> end
>
>Really, I'm writing this email not to praise or make fun, but to point
>out that the above technique may not work when 1.9 rolls around.
>(Assuming I remember correctly that 1.9 will/may change so that #send
>honors the public/protected/private state of the method being
>invoked.)

Correct. In 1.9 you need to use funcall() in place of send().

--
Mauricio Fernandez - http://eigenclass.org - singular Ruby