David A. Black ha scritto:
>> well, ri explicitly says for #extend:
>> " Adds to _obj_ the instance methods from each module given as a
>> parameter"
>> and this is omitting Constants, which are available when something is 
>> #include'd
> 
> 
> I'm not sure what you mean by "adding a constant to an object" if the
> object isn't a Class or Module.  For example:
> 
>   obj = Object.new
>   obj.constants    => no such method
> 
> All constants live in a class/module namespace.  That's what I mean
> when I say the closest you can get is adding it to an object's
> singleton class.

you're right as usual :)
what I meant is adding them *somewhere* so that they're available to the 
object. Basically I wondered if ruby had something able to do the 
singleton class hackery withouth me noticing it, like extend does for 
instance methods (but after reading the rest now I understand why this 
can't work)


>> NameError: uninitialized constant Foo2::Bar
> 
> 
> Where did "Foo2" come from?  I get Foo::Bar :-)

emh.. evil little creatures acting on my desktop, I'd say..

>>        from (irb):12:in `barer'
>>        from (irb):21
>>
>> I don't understand why, actually.
> 
> 
> I think it's this:  when an instance method refers to a constant, that
> constant is looked up in the class where the method is defined.
> Therefore, in your example, Foo#barer looks for Foo::Bar.
> 
> So it's not the same as a method call, where it would be looked up in
> the object's singleton class.  I guess that's because constants really
> belong to classes, not to objects generally.  So when you ask for a
> constant, the class you're in doesn't branch off and look in other
> classes.

ah, this makes a lot of sense, thanks


> This may have to do with the way constant references are parsed, which
> is kind of quasi-static.

mh.. maybe "lexical" is the better word ?

> Anyway, here's an illustration.
> 
> class Foo
>  def x
>    p X
>  end
> end
> 
> foo = Foo.new
> 
> class << foo
>   X = 1
>   Y = 1
>   def y
>     p Y
>   end
> end
> 
> foo.y         => 1  (that's <foo's singleton class>::Y)
> foo.x         => uninitialized constant Foo::X
>                     (foo's singleton class isn't involved)

thanks for the explanation, this makes sense now.