Semantics of << and <<-

Discussion in 'Ruby' started by Anders Höckersten, Aug 31, 2005.

  1. Hi,
    I recently joined this list, so please forgive me if this question has
    been asked recently. I am wondering about the precise semantics of <<
    and <<-. "Programming Ruby"[1] and the pseudo-BNFs[2][3] say that you
    can use a quoted string after <<. As I see it, this means I should be
    able to able to use the #{expr} construct inside this string, like this:
    print <<"#{2+2}"
    foobar
    #{4}

    This is, however, not the way my installation of Ruby (1.8.1) works.
    What I am wondering is, is this the expected behaviour and are both the
    book and the pseudo-BNFs wrong, or is this some form of bug in the
    interpreter?

    Best regards,
    Anders

    [1] Programming Ruby, 2nd Edition, p. 321
    [2] http://www.ruby-doc.org/docs/ruby-doc-
    bundle/Manual/man-1.4/yacc.html
    [3] http://www.ruby-lang.org/ja/man/?cmd=view;name=%B5%BF%BB%F7BNF%A4%CB
    %A4%E8%A4%EBRuby%A4%CE%CA%B8%CB%A1
     
    Anders Höckersten, Aug 31, 2005
    #1
    1. Advertising

  2. 2005/8/31, Anders H=F6ckersten <>:
    > Hi,
    > I recently joined this list, so please forgive me if this question has
    > been asked recently. I am wondering about the precise semantics of <<
    > and <<-. "Programming Ruby"[1] and the pseudo-BNFs[2][3] say that you
    > can use a quoted string after <<. As I see it, this means I should be
    > able to able to use the #{expr} construct inside this string, like this:
    > print <<"#{2+2}"
    > foobar
    > #{4}
    >=20
    > This is, however, not the way my installation of Ruby (1.8.1) works.
    > What I am wondering is, is this the expected behaviour and are both the
    > book and the pseudo-BNFs wrong, or is this some form of bug in the
    > interpreter?


    My guess would be that it's an omission in the documentation. I don't
    think you can do interpolation in the string. Basically it's not a
    Ruby string but an idendifier and the quotation announces differnt
    behaviro. After all, what do you gain by a computed terminator of a
    here document? I don't think that's useful.

    See http://www.ruby-doc.org/docs/ProgrammingRuby/#UD

    Kind regards

    robert
     
    Robert Klemme, Aug 31, 2005
    #2
    1. Advertising

  3. 2005/8/31, Robert Klemme <>:
    > 2005/8/31, Anders H=F6ckersten <>:
    > > Hi,
    > > I recently joined this list, so please forgive me if this question has
    > > been asked recently. I am wondering about the precise semantics of <<
    > > and <<-. "Programming Ruby"[1] and the pseudo-BNFs[2][3] say that you
    > > can use a quoted string after <<. As I see it, this means I should be
    > > able to able to use the #{expr} construct inside this string, like this=

    :
    > > print <<"#{2+2}"
    > > foobar
    > > #{4}
    > >
    > > This is, however, not the way my installation of Ruby (1.8.1) works.
    > > What I am wondering is, is this the expected behaviour and are both the
    > > book and the pseudo-BNFs wrong, or is this some form of bug in the
    > > interpreter?

    >=20
    > My guess would be that it's an omission in the documentation. I don't
    > think you can do interpolation in the string. Basically it's not a
    > Ruby string but an idendifier and the quotation announces differnt
    > behaviro. After all, what do you gain by a computed terminator of a
    > here document? I don't think that's useful.
    >=20
    > See http://www.ruby-doc.org/docs/ProgrammingRuby/#UD


    Here's the correct link:

    http://www.ruby-doc.org/docs/ProgrammingRuby/html/language.html#UD
     
    Robert Klemme, Aug 31, 2005
    #3
  4. Anders Höckersten wrote:
    > Hi,
    > I recently joined this list, so please forgive me if this question has
    > been asked recently. I am wondering about the precise semantics of <<
    > and <<-. "Programming Ruby"[1] and the pseudo-BNFs[2][3] say that you
    > can use a quoted string after <<. As I see it, this means I should be
    > able to able to use the #{expr} construct inside this string, like this:
    > print <<"#{2+2}"
    > foobar
    > #{4}


    The purpose of quoting the here-document label is to make the
    text be treated as though it were enclosed in single quotes.
    ----------------------------------------------
    puts <<'HERE'
    #{3**3} bells.
    HERE

    puts <<"HERE"
    #{3**3} bells.
    HERE

    puts <<HERE
    #{3**3} bells.
    HERE
    ------------------------------------------------

    #{3**3} bells.
    27 bells.
    27 bells.
     
    William James, Sep 1, 2005
    #4
    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. Andrew James
    Replies:
    1
    Views:
    993
    Diez B. Roggisch
    Dec 4, 2004
  2. Prof.Stanley

    SYNTAX AND SEMANTICS

    Prof.Stanley, May 27, 2006, in forum: C Programming
    Replies:
    6
    Views:
    378
    Dave Thompson
    Jun 8, 2006
  3. Replies:
    1
    Views:
    558
    Victor Bazarov
    Jun 24, 2005
  4. Prof.Stanley

    SYNTAX AND SEMANTICS

    Prof.Stanley, May 27, 2006, in forum: C++
    Replies:
    6
    Views:
    368
    benben
    May 28, 2006
  5. Mark Space

    Re: Set and .equals() semantics

    Mark Space, May 8, 2008, in forum: Java
    Replies:
    17
    Views:
    617
    Mark Space
    May 10, 2008
Loading...

Share This Page