From: Intransition on 7 Jun 2010 12:21 Thanks Robert and Caleb. At least it makes sense --even if it doesn't make sense ;-) > Ah, but constant lookup is supposed to check the ancestors if the > constant is not found in the scope. M should be among the ancestors of > o's singleton class, shouldn't it? I would have expected this to work. > OTOH, constant lookup is confusing. > > This way works: > > module M > class X; end > end > > o = Object.new # => #<Object:0x7fc61f1a1e58> > > (class << o; self; end).class_eval{ include M } # => #<Object:0x7fc61f1a1e58> > #or o.extend M as well, presumably > > class<<o > def x; X end > end > > o.x #=>M::X > > Seehttp://ruby.runpaint.org/variables#constantsin which the lookup > rules are summarized as: > (Module.nesting + container.ancestors + Object.ancestors).uniq > #slightly paraphrased > > I guess the key point to understand is that for singleton methods, > container is the (static) module of class enclosing the singleton > method, NOT the singleton class that they affect. I supposed, but that doesn't seem quite right, as the document pointed out that "self.ancestors" should be looked up. Shouldn't that catch the singleton class too? Honestly. Something is not right if these are not the same: class << o def x; X end end def o.x; X; end ~trans
First
|
Prev
|
Pages: 1 2 Prev: inject method of Array class Next: [ANN] io_splice 2.0.0 - IO::Splice.copy_stream fixes |