Legal syntax, bug or what?

Discussion in 'Ruby' started by Jonathan Maasland, Sep 1, 2006.

  1. Hi all,

    I'm still working on Ripper and during my explorations I came across a
    line of code in mkmf.rb that I can't wrap my head around.

    Line 1313
    ---
    opts = $arg_config.collect {|t, n| "\t#{t}#{"=#{n}" if n}\n"}
    ---

    When I copy-paste the entire method to a file and run it through Ruby
    everything is fine.
    However irb doesn't accept the method:
    ----
    irb(main):001:0> def mkmf_failed(path)
    irb(main):002:1> unless $makefile_created or File.exist?("Makefile")
    irb(main):003:2> opts = $arg_config.collect {|t, n| "\t#{t}#{"=#{n}"
    if n}\n"}
    irb(main):004:3* abort "*** #{path} failed ***\n" + FailedMessage +
    opts.join
    irb(main):005:3> end
    irb(main):006:2> end
    irb(main):007:1>
    ------
    As you can see irb doesn't detect the end of the third line.

    After taking the statement apart and simplifying it, it all boils down
    to the following construction:
    #{"=#{n}" if n}
    inside the ""main"" double-quoted string.

    Why does ruby accept this construct while irb doesn't?
    Doesn't Ruby see the first double-quote as the end of the string?

    Any clarification would be greatly appreciated.

    With kind regards
    Jonathan Maasland
     
    Jonathan Maasland, Sep 1, 2006
    #1
    1. Advertising

  2. Jonathan Maasland

    Guest

    Hi --

    On Fri, 1 Sep 2006, Jonathan Maasland wrote:

    > Hi all,
    >
    > I'm still working on Ripper and during my explorations I came across a line
    > of code in mkmf.rb that I can't wrap my head around.
    >
    > Line 1313
    > ---
    > opts = $arg_config.collect {|t, n| "\t#{t}#{"=#{n}" if n}\n"}
    > ---
    >
    > When I copy-paste the entire method to a file and run it through Ruby
    > everything is fine.
    > However irb doesn't accept the method:
    > ----
    > irb(main):001:0> def mkmf_failed(path)
    > irb(main):002:1> unless $makefile_created or File.exist?("Makefile")
    > irb(main):003:2> opts = $arg_config.collect {|t, n| "\t#{t}#{"=#{n}" if
    > n}\n"}
    > irb(main):004:3* abort "*** #{path} failed ***\n" + FailedMessage +
    > opts.join
    > irb(main):005:3> end
    > irb(main):006:2> end
    > irb(main):007:1>
    > ------
    > As you can see irb doesn't detect the end of the third line.
    >
    > After taking the statement apart and simplifying it, it all boils down to the
    > following construction:
    > #{"=#{n}" if n}
    > inside the ""main"" double-quoted string.
    >
    > Why does ruby accept this construct while irb doesn't?
    > Doesn't Ruby see the first double-quote as the end of the string?
    >
    > Any clarification would be greatly appreciated.


    Nested #{} are allowed, so this is some kind of irb parsing glitch.
    It seems to be the = that's confusing irb for some reason:

    irb(main):002:0> "#{"hello#{}"}"
    => "hello"
    irb(main):003:0> "#{"hello=#{}"}"
    irb(main):004:0*

    I believe it's causing the second # to be interpreted as introducing a
    comment.


    David

    --
    David A. Black |
    Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
    DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
    [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
    [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
     
    , Sep 1, 2006
    #2
    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. Boltar

    Legal C or bug in gcc

    Boltar, Feb 27, 2008, in forum: C Programming
    Replies:
    31
    Views:
    866
    Richard
    Mar 1, 2008
  2. rickman
    Replies:
    1
    Views:
    1,071
    rickman
    Feb 21, 2010
  3. gabriele renzi
    Replies:
    2
    Views:
    240
    gabriele renzi
    Dec 31, 2005
  4. Ken Bloom
    Replies:
    3
    Views:
    244
  5. Good Night Moon
    Replies:
    9
    Views:
    316
    Rick DeNatale
    Jul 25, 2007
Loading...

Share This Page