# What this code means?

**URL:** <https://www.ruby-forum.com/t/what-this-code-means/127141>\
**Category:** Ruby\
**Created:** [January 25, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/what-this-code-means/127141 "2008-01-25T12:05:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [January 25, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/what-this-code-means/127141/1 "2008-01-25T12:05:40Z")

</div>

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

module YourLastModule  
class \<\< self  
def included©  
class \<\< c  
def say\_hellol(name)  
return "Hello, " + name  
end  
end  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![MohsinHijazee](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e68b1a/32.png) [@MohsinHijazee](https://www.ruby-forum.com/u/MohsinHijazee)\
**Post date:** [January 25, 2008, 12:43pm UTC](https://www.ruby-forum.com/t/what-this-code-means/127141/2 "2008-01-25T12:43:48Z")

</div>

2008/1/25, MohsinHijazee [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
