From: Alexandru Popescu Date: 2005-08-22T02:44:08+09:00 Subject: Re: include and extend Module - Ruby Way #: Gavin Kistner changed the world a bit at a time by saying on 8/21/2005 7:29 PM :# > On Aug 21, 2005, at 9:04 AM, Alexandru Popescu wrote: >>> Sorry, I don't know Java (any more). Are you describing a global >>> function that is only available to instance methods of a certain >>> class, which is invoked without an explicit receiver? >>> >> >> Nope. I was talking about: >> [code] >> public class ExampleClass { >> public static void staticMethod() { >> // illegal access to any instance field >> System.out.println("staticMethod"); >> } >> } >> >> ExampleClass.staticMethod(); // prints the message; no instance of >> ExampleClass is available >> ExampleClass ec= new ExampleClass(); >> ec.staticMethod(); // prints the message; not recommended but works > > I don't know of a built-in mechanism to do this in Ruby, other than > something like: > > class Foo > def self.staticMethod > puts "staticMethod" > end > def staticMethod > self.class.staticMethod > end > end > > There are things you could do to make it easier to set this up, so > you might only have to type something like: > class Foo > static_method :foo { > # your_code_here > } > end > but that would just be a fancy interface to setting up a method on > both the class and its instances. > > > You say that it's not recommended, so I gather this isn't a big > issue. Is there some specific use case where you find this useful? > > Actually I was trying to clarify my mind. No, I am not gonna use such a thing (taking into account that however it is wrong). :alex |.::the_mindstorm::.|