From: David Alan Black Date: 2001-12-09T03:31:23+09:00 Subject: [ruby-talk:27953] Re: Module and object extend. Can someone please explain Hi -- On Sun, 9 Dec 2001, Jim Freeze wrote: > Hi > > Ok, I thought that I had done this in the past, but > now I cannot seem to get it to work. > Can someone explain how to extend an object and > access a Constant? > > module M; C=2; def c; puts "c"; end; end > nil > irb(main):002:0> class A; end > nil > irb(main):003:0> a=A.new > # > irb(main):004:0> a.extend(M) > # > irb(main):005:0> a.c > c # => good, this works > nil > irb(main):006:0> a.C > NameError: undefined method `C' for # # => ?? > from (irb):6 > irb(main):007:0> a::C # => ?? How does a access C? > NameError: undefined method `C' for # > from (irb):7 I don't think #extend will give you access to constants defined in a module: $ ri extend ---------------------------------------------------------- Object#extend obj.extend( [aModule]+> ) -> obj ------------------------------------------------------------------------ Adds to obj the instance methods from each module given as a parameter. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav