Martin Coxall a ñÄrit :
> On 10/2/06, Jan Svitok <jan.svitok / gmail.com> wrote:
>>
>> On 10/2/06, Luke Stark <Luke.Stark / mathworks.com> wrote:
>> > You may create singleton methods like so:
>> >
> 
> 
> I'll have one more go. This time I'll get it right. ;p
> 
> obj.class.instance_eval do
>   define_method(:method_name) do |*args|
>      # Method goes here
>   end
> end
> 
> N.B. this actually works this time, because I tried it.
> 
> Regards,
> 
> Martin

Hi,

You can use class_eval, or better no *eval function :

$ cat my_thing.rb
#!/usr/bin/env ruby

class MyThing; end
foo = MyThing.new

class << foo
   bar = "do_stuff"
   define_method(bar) { "code" }
end

puts foo.do_stuff

$ ruby my_thing.rb
code

-- 
Bruno Michel