Problem with HERE document as first parameter (parser bug?)

Discussion in 'Ruby' started by Ronald Fischer, Jul 11, 2007.

  1. I have problems passing a HERE document as the first parameter
    to a function expecting more than 1 parameter. Example:

    # This is file here1.rb
    def q(a,b)
    end
    q(<<-END
    line1
    line2
    END
    ,'x')


    Executing this program yields the error messages

    /here1.rb:9: syntax error, unexpected ',', expecting ')'
    ,'x')
    ^
    /here1.rb:9: syntax error, unexpected ')', expecting $end


    Using a temporary variable to hold the content of the HERE
    string works fine though:

    temp=3D<<-END
    line1
    line2
    END
    q(temp,'x')


    Bug in Ruby? Or do I misunderstand something in the workings of the
    parser?

    Ronald
    --=20
    Ronald Fischer <>
    Phone: +49-89-452133-162
    =20
    Ronald Fischer, Jul 11, 2007
    #1
    1. Advertising

  2. Re: Problem with HERE document as first parameter (parser bu

    Ronald Fischer wrote:
    > I have problems passing a HERE document as the first parameter
    > to a function expecting more than 1 parameter. Example:
    >
    > # This is file here1.rb
    > def q(a,b)
    > end
    > q(<<-END
    > line1
    > line2
    > END
    > ,'x')
    >
    >
    > Executing this program yields the error messages
    >
    > ./here1.rb:9: syntax error, unexpected ',', expecting ')'
    > ,'x')
    > ^
    > ./here1.rb:9: syntax error, unexpected ')', expecting $end
    >
    >
    > Using a temporary variable to hold the content of the HERE
    > string works fine though:
    >
    > temp=<<-END
    > line1
    > line2
    > END
    > q(temp,'x')
    >
    >
    > Bug in Ruby? Or do I misunderstand something in the workings of the
    > parser?
    >
    > Ronald


    Not a bug in the parser, just wrong usage.
    q(<<-END, 'x')
    line1
    line2
    END

    That will work.

    Regards
    Stefan

    --
    Posted via http://www.ruby-forum.com/.
    Stefan Rusterholz, Jul 11, 2007
    #2
    1. Advertising

  3. Ronald Fischer

    John Joyce Guest

    Re: Problem with HERE document as first parameter (parser bu

    On Jul 11, 2007, at 6:53 AM, Stefan Rusterholz wrote:

    > Ronald Fischer wrote:
    >> I have problems passing a HERE document as the first parameter
    >> to a function expecting more than 1 parameter. Example:
    >>
    >> # This is file here1.rb
    >> def q(a,b)
    >> end
    >> q(<<-END
    >> line1
    >> line2
    >> END
    >> ,'x')
    >>
    >>
    >> Executing this program yields the error messages
    >>
    >> ./here1.rb:9: syntax error, unexpected ',', expecting ')'
    >> ,'x')
    >> ^
    >> ./here1.rb:9: syntax error, unexpected ')', expecting $end
    >>
    >>
    >> Using a temporary variable to hold the content of the HERE
    >> string works fine though:
    >>
    >> temp=<<-END
    >> line1
    >> line2
    >> END
    >> q(temp,'x')
    >>
    >>
    >> Bug in Ruby? Or do I misunderstand something in the workings of the
    >> parser?
    >>
    >> Ronald

    >
    > Not a bug in the parser, just wrong usage.
    > q(<<-END, 'x')
    > line1
    > line2
    > END
    >
    > That will work.
    >
    > Regards
    > Stefan
    >

    While it works, it is, IMHO, ugly and a little obfuscated.
    It is much better to simply assign the heredoc to a variable, and put
    the variable name in the function parameter.
    I know it is a correct form, but some times linguistically correct is
    not always good for you. (most people know what I mean if I mention C )

    John Joyce
    John Joyce, Jul 11, 2007
    #3
  4. 2007/7/11, Ronald Fischer <>:
    > I have problems passing a HERE document as the first parameter
    > to a function expecting more than 1 parameter. Example:
    >
    > # This is file here1.rb
    > def q(a,b)
    > end
    > q(<<-END
    > line1
    > line2
    > END
    > ,'x')
    >
    >
    > Executing this program yields the error messages
    >
    > ./here1.rb:9: syntax error, unexpected ',', expecting ')'
    > ,'x')
    > ^
    > ./here1.rb:9: syntax error, unexpected ')', expecting $end


    I believe you have got the order wrong. Do it like this:

    $ ruby -e 'def f(a,b) p a,b end
    > f(<<XXX, 123)
    > foo
    > bar
    > XXX'

    "foo\nbar\n"
    123

    > Using a temporary variable to hold the content of the HERE
    > string works fine though:
    >
    > temp=<<-END
    > line1
    > line2
    > END
    > q(temp,'x')
    >
    >
    > Bug in Ruby? Or do I misunderstand something in the workings of the
    > parser?


    The latter, see above. :)

    Kind regards

    robert
    Robert Klemme, Jul 11, 2007
    #4
  5. Re: Problem with HERE document as first parameter (parser bu

    Valeri Mytinski wrote:
    > irb(main):072:0> def q(a,b)
    > irb(main):073:1> puts a
    > irb(main):074:1> puts b
    > irb(main):075:1> end
    > => nil
    > irb(main):076:0> q('1',<<-END
    > irb(main):077:1" 2
    > irb(main):078:1" END
    > irb(main):079:1> )
    > 1
    > 2
    > => nil
    >
    > If correctness of usage depends on parameters order - is this a bug?


    I'm actually suprised, that your usage works.

    puts('1', <<END)
    2
    END

    That's how I'd have written it.
    You can also use multiple

    puts(<<FIRST, <<SECOND)
    1
    FIRST
    2
    SECOND

    Or apply methods

    puts(<<FIRST.upcase, <<SECOND.capitalize)
    first
    FIRST
    second
    SECOND
    # (will print "FIRST" newline "Second")

    Btw, no need for the 'q' method, puts can handle multiple arguments ;-)

    Regards
    Stefan

    --
    Posted via http://www.ruby-forum.com/.
    Stefan Rusterholz, Jul 11, 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. Stuart Redmann
    Replies:
    5
    Views:
    477
    Stuart Redmann
    Dec 14, 2007
  2. George Hester

    Try over here likely more to the point here

    George Hester, Sep 30, 2004, in forum: Javascript
    Replies:
    0
    Views:
    110
    George Hester
    Sep 30, 2004
  3. Abdul Mohsin
    Replies:
    1
    Views:
    263
  4. FAQ server
    Replies:
    0
    Views:
    156
    FAQ server
    Aug 10, 2006
  5. FAQ server
    Replies:
    0
    Views:
    130
    FAQ server
    Oct 7, 2006
Loading...

Share This Page