From: Tamara Temple Date: 2013-07-23T14:23:10+09:00 Subject: Re: How can we create instance method and class methods at runtime in ruby? On Jul 22, 2013, at 11:43 PM, Love U Ruby wrote: > How can we create instance method and class methods at runtime in ruby? > > class Foo > def test > p "hi" > end > end > foo = Foo.new > foo.test # => "hi" > class Foo > def test > p "hiyy" > end > end > foo.test > # >> "hi" > # >> "hiyy" > > How Can I re-define the `test` method at runtime? I tried > `define_method`,but it didn't work for this. > > -- > Posted via http://www.ruby-forum.com/. > Don't know what you're doing, but this works: $ irb 2.0.0p247 :001 > class Foo; def test; p "hi"; end;end => nil 2.0.0p247 :002 > foo = Foo.new => # 2.0.0p247 :003 > foo.test "hi" => "hi" 2.0.0p247 :004 > class Foo; def test; p "bye"; end;end => nil 2.0.0p247 :005 > foo.test "bye" => "bye" 2.0.0p247 :006 > class Foo; def self.test; p "bazinga!"; end;end => nil 2.0.0p247 :007 > Foo.test "bazinga!" => "bazinga!" 2.0.0p247 :008 > foo.test "bye" => "bye"