[LRUG] Ruby Cluetrain
Tim Cowlishaw
tim at timcowlishaw.co.uk
Mon Oct 26 07:04:33 PDT 2009
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
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
More information about the Chat
mailing list