# Code explanation requeted

**URL:** <https://rubytalk.org/t/code-explanation-requeted/35100>\
**Category:** ruby-talk\
**Created:** [5 February 2007 23:32 UTC](https://rubytalk.org/t/code-explanation-requeted/35100 "2007-02-05T23:32:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neville\_Franks](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Neville\_Franks](https://rubytalk.org/u/Neville_Franks)\
**Post date:** [5 February 2007 23:32 UTC](https://rubytalk.org/t/code-explanation-requeted/35100/1 "2007-02-05T23:32:53Z")

</div>

Hi, Can someone please shed some light on the following code:

> **···**
>
> ---  
> &nbsp;&nbsp;def test\_listen  
> &nbsp;&nbsp;&nbsp;&nbsp;s = nil  
> &nbsp;&nbsp;&nbsp;&nbsp;log = Object.new  
> &nbsp;&nbsp;&nbsp;&nbsp;class \<\< log; self end.\_\_send\_\_(:define\_method, :to\_int) {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.close  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2  
> &nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;inet\_stream do |s|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_raise(IOError) {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.listen(log)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> ---
> 
> In particular the "class" statement and "self  
> end.\_\_send\_\_(:define\_method, :to\_int)"
> 
> Is this actually a class and where is its "end"?
> 
> I didn't know you can have a class inside a method. So I'm confused here  
> as well.
> 
> This code is messing up the Ruby parser I'm working on in my IDE.
> 
> ---  
> Neville Franks, [http://www.getsoft.com](http://www.getsoft.com) [http://www.surfulater.com](http://www.surfulater.com)
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [5 February 2007 23:39 UTC](https://rubytalk.org/t/code-explanation-requeted/35100/2 "2007-02-05T23:39:10Z")

</div>

Hi --

> Hi, Can someone please shed some light on the following code:
> 
> ---  
> def test\_listen  
> &nbsp;&nbsp;&nbsp;s = nil  
> &nbsp;&nbsp;&nbsp;log = Object.new  
> &nbsp;&nbsp;&nbsp;class \<\< log; self end.\_\_send\_\_(:define\_method, :to\_int) {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.close  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;2  
> &nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;inet\_stream do |s|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;assert\_raise(IOError) {  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s.listen(log)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}  
> &nbsp;&nbsp;&nbsp;end  
> end  
> ---
> 
> In particular the "class" statement and "self  
> end.\_\_send\_\_(:define\_method, :to\_int)"
> 
> Is this actually a class and where is its "end"?

It is a class; it's the singleton class of log. You may join the club  
of people who want a singleton\_class method 🙂 That would make it:

&nbsp;&nbsp;&nbsp;log.singleton\_class.\_\_send\_\_(:define\_method, :to\_int) ...

As it stands, in order to address the singleton class as an object,  
you have to capture it through the technique of:

&nbsp;&nbsp;&nbsp;class \<\< log  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self  
&nbsp;&nbsp;&nbsp;end

What you're seeing in the code you've got is this, but strung together  
on one line without much to help you parse it visually if you don't  
already know what to expect. I would tend to write it as:

&nbsp;&nbsp;&nbsp;(class \<\< log; self; end).\_\_send\_\_ etc.

David

> **···**
>
> On Tue, 6 Feb 2007, Neville Franks wrote:
> 
> --  
> Q. What is THE Ruby book for Rails developers?  
> A. RUBY FOR RAILS by David A. Black ([http://www.manning.com/black\](http://www.manning.com/black%5C))  
> &nbsp;&nbsp;&nbsp;&nbsp;(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf\](http://www.rubypal.com/r4rrevs.pdf%5C))  
> Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
> A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))

---

<div class="post-metadata">

**Author:** ![Gary\_Wright](https://avatars.discourse-cdn.com/v4/letter/g/58956e/32.png) [@Gary\_Wright](https://rubytalk.org/u/Gary_Wright)\
**Post date:** [6 February 2007 00:04 UTC](https://rubytalk.org/t/code-explanation-requeted/35100/3 "2007-02-06T00:04:28Z")

</div>

I'd add that

&nbsp;&nbsp;&nbsp;\_\_send\_\_(:define\_method, :to\_int)

is used instead of simply:

&nbsp;&nbsp;&nbsp;define\_method(:to\_int)

to override the private status of 'define\_method'.

Gary Wright

> **···**
>
> On Feb 5, 2007, at 6:39 PM, dblack@wobblini.net wrote:
> 
> > What you're seeing in the code you've got is this, but strung together  
> > on one line without much to help you parse it visually if you don't  
> > already know what to expect. I would tend to write it as:
> > 
> > &nbsp;&nbsp;(class \<\< log; self; end).\_\_send\_\_ etc.

---

<div class="post-metadata">

**Author:** ![Neville\_Franks](https://avatars.discourse-cdn.com/v4/letter/n/df705f/32.png) [@Neville\_Franks](https://rubytalk.org/u/Neville_Franks)\
**Post date:** [6 February 2007 03:15 UTC](https://rubytalk.org/t/code-explanation-requeted/35100/4 "2007-02-06T03:15:52Z")

</div>

unknown wrote:

> ..  
> What you're seeing in the code you've got is this, but strung together  
> on one line without much to help you parse it visually if you don't  
> already know what to expect. I would tend to write it as:
> 
> &nbsp;&nbsp;&nbsp;(class \<\< log; self; end).\_\_send\_\_ etc.
> 
> David

Thanks David. And there I was thinking us C++ folks were good at writing  
incomprehsible code. 🙂

> **···**
>
> ---  
> Neville Franks, [http://www.getsoft.com](http://www.getsoft.com) [http://www.surfulater.com](http://www.surfulater.com)
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Marcello\_Barnaba](https://avatars.discourse-cdn.com/v4/letter/m/f0a364/32.png) [@Marcello\_Barnaba](https://rubytalk.org/u/Marcello_Barnaba)\
**Post date:** [6 February 2007 18:58 UTC](https://rubytalk.org/t/code-explanation-requeted/35100/5 "2007-02-06T18:58:32Z")

</div>

🙂 i know it may sound overkill, but if you really want to understand what the  
heck these singleton/eigen classes are, this one is for you:

[http://rhg.rubyforge.org/chapter04.html](http://rhg.rubyforge.org/chapter04.html)

but maybe you should also read the previous chapters as well, it shouldn't  
take more than some hours to have the big picture clear in your mind.

the documentation is based upon ruby 1.7.3, IIRC, and I don't know what's  
changed later, but I'm sure that the overall cleanness of MRI hasn't been  
altered :).

def post\_scriptum; puts yield end  
post\_scriptum do %{  
hello list! this is my first post!  
lurking from some months, I learned a lot from you all!  
thanks!  
} end

> **···**
>
> On Tuesday 06 February 2007 04:15, Neville Franks wrote:
> 
> > \> (class \<\< log; self; end).\_\_send\_\_ etc.
> 
> > Thanks David. And there I was thinking us C++ folks were good at writing  
> > incomprehsible code. 🙂
> 
> --  
> pub 1024D/8D2787EF 723C 7CA3 3C19 2ACE 6E20 9CC1 9956 EB3C 8D27 87EF
