From: Tom Sawyer Date: 2002-09-12T18:16:03+09:00 Subject: Re: New newbies Qs on Ruby On Thu, 2002-09-12 at 02:59, Overnight wrote: > Both work, but I feel like none is the right one... The first solution > requires creating and than extending an object, just for the purpose of > having something to which "attach" M's instance methods, and it looks > ugly. The second solution looks more "clean" and maybe I'm just not OO > enough to accept it :-) but IMHO m1 and m2 would "look better" as > instance methods. > > So... is there another "right" way to get what I want? your third is right. you can use def self.m1 instead from what i understand, though i never do. and self will pick up the module at that level. that's how it works ruby allows you to define module/class methods by prefixing the module/class name and a period. honest question, how would you want it to work? -tom > > 2) In "Programming Ruby", it is said that the global availability of > functions like 'Array', 'open', 'putc', 'sleep', etc., depends on the > fact that such functions are module methods of Kernel, and that Kernel is > included by Object. In fact, 'Kernel.singleton_methods.sort' returns: > > ['Array', 'Float', 'Integer', ...] > > However, also 'Kernel.private_instance_methods.sort' returns: > > ['Array', 'Float', 'Integer', ...] > > and the two sets are almost identical: > > irb(main):005:0> Kernel.singleton_methods-Kernel.private_instance_methods > [] > irb(main):006:0> Kernel.private_instance_methods-Kernel.singleton_methods > ["remove_instance_variable"] > > The functions actually used when called without an explicit receiver are > (of course) the private instance methods: > > irb(main):001:0> module Kernel > irb(main):002:1> def sleep > irb(main):003:2> puts "No thanks, I'm not tired" > irb(main):004:2> end > irb(main):005:1> end > nil > irb(main):006:0> sleep > No thanks, I'm not tired > nil > irb(main):007:0> Kernel.sleep > <> > IRB::Abort: abort then interrupt!! > from E:/RUBY/lib/ruby/1.6/irb.rb:79:in `irb_abort' > from E:/RUBY/lib/ruby/1.6/irb.rb:214:in `signal_handle' > from E:/RUBY/lib/ruby/1.6/irb.rb:64:in `start' > from E:/RUBY/lib/ruby/1.6/irb.rb:63:in `call' > from (irb):7:in `sleep' > from (irb):7 > > So my question is: why are the functions defined as both singletons and > private instance methods? To allow the coexistence of both > 'Kernel.function()' and the 'function()' syntaxes? Or to allow > possible redefinitions of the instance methods in terms of the singletons > methods? Or... what else? > > Thank you for your help, > > Andrea > -- tom sawyer, aka transami transami@transami.net