From: Robert Dober Date: 2007-03-14T20:43:34+09:00 Subject: Re: how to add a "static method" dynamic ? On 3/14/07, Yan Rong wrote: > now , i know that i can use > "define_method" add a method dynamic, > but i want to know how to add a "static method" dynamic ? > > thanks! > > -- > Posted via http://www.ruby-forum.com/. > > There are no static methods, there are only instance methods. But as about everything in Ruby is an Object, so is the class of your object and so is the singleton class of your object, maybe the following tricks are what you are looking for: 707/475 > cat class.rb && ruby class.rb # vim: sts=2 sw=2 tw=0 expandtab nu: class A define_method :one do |p| "one(#{p})" end class << self define_method :two do |p| "two(#{p})" end end eval 'def self.three p ; "three(#{p})" end' end puts A.new.one(42) puts A.two(42) puts A.three(42) one(42) two(42) three(42) Sorry for the double post Erik but I was not about to discard all that hard work ;) Please note the ugliness of the eval for :three but it gets the job done. Personally I almost always use the singleton class method definition even when declaring statically, but I believe that it is not *exactly* the same thing. Cheers Robert -- You see things; and you say Why? But I dream things that never were; and I say Why not? -- George Bernard Shaw