[LRUG] Ruby Cluetrain

Matthew Rudy Jacobs matthewrudyjacobs at gmail.com
Mon Oct 26 07:15:41 PDT 2009


well not quite.
If you do the same code without an include it works

here's an example http://gist.github.com/218686

so the const_get bubbles up the current namespace in normal circumstances,
but with an include it doesnt work.

2009/10/26 Tim Cowlishaw <tim at timcowlishaw.co.uk>

> Hmm - Mod::Child.cry or possibly ::Mod::Child.cry rather than just
> Child.cry - or am I missing something more subtle? Child isn't set in the
> global namespace or within the Behaviour module, which seems to be the
> reason for the error.
>
> Cheers,
>
> Tim


But I would have thought that const_get would traverse namespaces from the
current

>
>
> On 26 Oct 2009, at 13:58, Matthew Rudy Jacobs wrote:
>
>  I rewrote it without all the crazy Module.new and Class.new stuff
>> and it still doesnt make any sense
>>
>> http://gist.github.com/218649
>>
>> 2009/10/26 Anthony Green <Anthony.Green at bbc.co.uk>
>>
>> I hope James won't mind me posting this Gist he tweeted at the weekend.
>> But
>> it exemplifies the kind of gaps I have in my Ruby knowledge.
>> Whilst I understand what individual segments of code are doing I don't
>> understand what the code as a whole is attempting to do.
>>
>> Anyone willing to shed some light ?
>>
>> http://gist.github.com/217001
>>
>>
>> # I'm creating some classes and modules on the fly, but things aren't
>> hooking up quite the way I'd hoped.
>> # Can anyone explain why when an instance of Mod::Sibling tries to call
>>
>> Child.cry, that it
>>
>> # doesn't call Mod::Child.cry, but instead looks for Behaviour::Child?
>>
>> class Base
>>  def self.cry
>>    "#{self.name}.cry called!"
>>  end
>> end
>>
>> module Behaviour
>>  def blame
>>    puts "I am #{self}, a #{self.class.name}, trying to call Child.cry"
>>    Child.cry
>>  end
>> end
>>
>> child = Class.new(Base)
>>
>> Mod = Module.new
>> Mod.const_set(:Child, child)
>>
>> child.cry # => "Mod::Child.cry called!"
>> Mod::Child.cry # => "Mod::Child.cry called!"
>> child.name # => "Mod::Child"
>>
>> sibling = Class.new(Base)
>> sibling.class_eval do
>>  include Behaviour
>> end
>> Mod.const_set(:Sibling, sibling)
>> sibling.name # => "Mod::Sibling"
>>
>> Mod.constants # => ["Child", "Sibling"]
>>
>> sibling = Mod::Sibling.new
>> sibling.blame # =>
>> # ~> -:10:in `blame': uninitialized constant Behaviour::Child (NameError)
>> # ~>  from -:32
>> # >> I am #<Mod::Sibling:0x1973078>, a Mod::Sibling, trying to call
>> Child.cry
>>
>>
>>
>>
>>
>> --
>> Anthony Green
>> Client Side Developer
>> Future Media & Technology for BBC Audio & Music Interactive
>>
>>
>> http://www.bbc.co.uk/
>> This e-mail (and any attachments) is confidential and may contain personal
>> views which are not the views of the BBC unless specifically stated.
>> If you have received it in error, please delete it from your system.
>> Do not use, copy or disclose the information in any way nor act in
>> reliance on it and notify the sender immediately.
>> Please note that the BBC monitors e-mails sent or received.
>> Further communication will signify your consent to this.
>>
>> _______________________________________________
>> Chat mailing list
>> Chat at lists.lrug.org
>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>>
>> _______________________________________________
>> Chat mailing list
>> Chat at lists.lrug.org
>> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>>
>
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20091026/388c703f/attachment-0003.html>


More information about the Chat mailing list