From: dblack@... Date: 2007-02-06T08:39:10+09:00 Subject: Re: Code explanation requeted Hi -- On Tue, 6 Feb 2007, Neville Franks wrote: > 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"? 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 -- Q. What is THE Ruby book for Rails developers? A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black) (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf) Q. Where can I get Ruby/Rails on-site training, consulting, coaching? A. Ruby Power and Light, LLC (http://www.rubypal.com)