Code explanation requeted

Discussion in 'Ruby' started by Neville Franks, Feb 5, 2007.

  1. 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 Franks, http://www.getsoft.com http://www.surfulater.com

    --
    Posted via http://www.ruby-forum.com/.
     
    Neville Franks, Feb 5, 2007
    #1
    1. Advertising

  2. Neville Franks

    Guest

    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)
     
    , Feb 5, 2007
    #2
    1. Advertising

  3. Neville Franks

    Guest

    On Feb 5, 2007, at 6:39 PM, 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 Wright
     
    , Feb 6, 2007
    #3
  4. 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 Franks, http://www.getsoft.com http://www.surfulater.com

    --
    Posted via http://www.ruby-forum.com/.
     
    Neville Franks, Feb 6, 2007
    #4
  5. 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. :)


    :) 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

    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
    --
    pub 1024D/8D2787EF 723C 7CA3 3C19 2ACE 6E20 9CC1 9956 EB3C 8D27 87EF
     
    Marcello Barnaba, Feb 6, 2007
    #5
    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. uthra
    Replies:
    1
    Views:
    419
    John Gordon
    Nov 10, 2006
  2. responsible
    Replies:
    4
    Views:
    352
    red floyd
    Aug 5, 2008
  3. Michael Albers

    Explanation on some code

    Michael Albers, Dec 1, 2008, in forum: Ruby
    Replies:
    5
    Views:
    123
    Robert Dober
    Dec 1, 2008
  4. Phil Thompson

    Explanation of code needed

    Phil Thompson, Mar 21, 2005, in forum: Javascript
    Replies:
    4
    Views:
    167
    Phil Thompson
    Mar 22, 2005
  5. Replies:
    1
    Views:
    77
    Richard Cornford
    Apr 18, 2008
Loading...

Share This Page