# Code explanation requeted

**URL:** <https://www.ruby-forum.com/t/code-explanation-requeted/84558>\
**Category:** Ruby\
**Created:** [February 6, 2007, 12:32am UTC](https://www.ruby-forum.com/t/code-explanation-requeted/84558 "2007-02-06T00:32:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nevf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/97f17d/32.png) [@nevf](https://www.ruby-forum.com/u/nevf)\
**Post date:** [February 6, 2007, 12:32am UTC](https://www.ruby-forum.com/t/code-explanation-requeted/84558/1 "2007-02-06T00:32:47Z")

</div>

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

* * *

## def test\_listen s = nil log = Object.new class \<\< log; self end. **send** (:define\_method, :to\_int) { s.close 2 } inet\_stream do |s| assert\_raise(IOError) { s.listen(log) } 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”?

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 F., [http://www.getsoft.com](http://www.getsoft.com) [http://www.surfulater.com](http://www.surfulater.com)

---

<div class="post-metadata">

**Author:** ![nevf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/97f17d/32.png) [@nevf](https://www.ruby-forum.com/u/nevf)\
**Post date:** [February 6, 2007, 1:06am UTC](https://www.ruby-forum.com/t/code-explanation-requeted/84558/2 "2007-02-06T01:06:37Z")

</div>

Hi –

On Tue, 6 Feb 2007, Neville F. wrote:

> inet\_stream do |s|  
> 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:

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:

class \<\< log  
self  
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:

(class \<\< log; self; end). **send** etc.

David

---

<div class="post-metadata">

**Author:** ![nevf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/97f17d/32.png) [@nevf](https://www.ruby-forum.com/u/nevf)\
**Post date:** [February 6, 2007, 4:15am UTC](https://www.ruby-forum.com/t/code-explanation-requeted/84558/3 "2007-02-06T04:15:49Z")

</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:
> 
> (class \<\< log; self; end). **send** etc.
> 
> David

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

* * *

Neville F., [http://www.getsoft.com](http://www.getsoft.com) [http://www.surfulater.com](http://www.surfulater.com)

---

<div class="post-metadata">

**Author:** ![nevf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/97f17d/32.png) [@nevf](https://www.ruby-forum.com/u/nevf)\
**Post date:** [February 6, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/code-explanation-requeted/84558/4 "2007-02-06T19:58:55Z")

</div>

On Tuesday 06 February 2007 04:15, Neville F. wrote:

> > (class \<\< log; self; end). **send** etc.

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

🙂 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

---

<div class="post-metadata">

**Author:** ![nevf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/97f17d/32.png) [@nevf](https://www.ruby-forum.com/u/nevf)\
**Post date:** [September 25, 2007, 11:10pm UTC](https://www.ruby-forum.com/t/code-explanation-requeted/84558/5 "2007-09-25T23:10:36Z")

</div>

On Feb 5, 2007, at 6:39 PM, removed\_email\_address@domain.invalid 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:
> 
> (class \<\< log; self; end). **send** etc.

I’d add that

**send** (:define\_method, :to\_int)

is used instead of simply:

define\_method(:to\_int)

to override the private status of ‘define\_method’.

Gary W.
