What this code means?

Discussion in 'Ruby' started by MohsinHijazee, Jan 25, 2008.

  1. Hello!
    I am new to Ruby, can anyone explain what this code snippet is
    trying to do?


    module YourLastModule
    class << self
    def included(c)
    class << c
    def say_hellol(name)
    return "Hello, " + name
    end
    end
    end
    end
    end
     
    MohsinHijazee, Jan 25, 2008
    #1
    1. Advertising

  2. MohsinHijazee

    Stefan Lang Guest

    2008/1/25, MohsinHijazee <>:
    > Hello!


    Hello!

    > I am new to Ruby, can anyone explain what this code snippet is
    > trying to do?


    First you have to understand the following Ruby construct:

    class << some_object
    def hello
    puts "hello"
    end
    end

    The "class << some_object" line opens the "eigenclass"
    of some_object. The eigenclass is Class object that
    belongs solely to some_object and thus we're not
    interfering with any other objects class. All methods
    added in "class << some_object" belong only
    to some_object.

    some_object.hello # prints "hello"

    Now to your example:

    module YourLastModule

    # At this point, self refers to YourLastModule

    # Thus here we are opening the eigenclass
    # of YourLastModule.
    class << self

    # This method is defined on the YourLastModule
    # object (and _not_ on instances of YourLastModule).
    #
    # The included method of a module is called by
    # Ruby when the module is included in a class
    # or another module. The argument c is the including
    # class or module.
    def included(c)

    # Here we open up the eigenclass of the including
    # class.
    class << c

    # Adding a method to the including class object
    # (the method is _not_ available to instances of the class).
    def say_hello(name)
    return "Hello, " + name
    end

    end
    end
    end
    end

    In effect, the example demonstrates how to add
    class methods to including classes.

    class Foo
    include YourLastModule
    end

    Foo.say_hello("Arthur") # => "Hello, Arthur"

    I'd say this stuff belongs into the advanced section
    of ruby learning material.

    HTH,
    Stefan
     
    Stefan Lang, Jan 25, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rookie
    Replies:
    4
    Views:
    14,807
    Chris Langsenkamp
    Aug 20, 2003
  2. clintonG

    Re: What means Protected WithEvents ?

    clintonG, Jul 5, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    471
    clintonG
    Jul 5, 2003
  3. Andreas Klemt
    Replies:
    2
    Views:
    360
  4. David Stockwell
    Replies:
    1
    Views:
    549
    Grant Edwards
    May 28, 2004
  5. Replies:
    14
    Views:
    662
    Neelesh Bodas
    Jul 11, 2007
Loading...

Share This Page