[LRUG] Ruby Cluetrain

Anthony Green Anthony.Green at bbc.co.uk
Mon Oct 26 06:34:50 PDT 2009


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.
					



More information about the Chat mailing list