Ruby newbie newline question

Discussion in 'Ruby' started by Gary McGath, Oct 8, 2012.

  1. Gary McGath

    Gary McGath Guest

    I'm an experienced programmer but a complete beginner at Ruby.

    I've tried to find an explanation of how newlines affect Ruby syntax,
    without success. Some websites claim that Ruby doesn't care about
    newlines, which is clearly false.

    The following code works:

    if i == 1
    print "one"
    elsif i == 2
    print "two"
    end

    If I put the same code all on one line, it gets an error ("unexpected
    kELSIF, expecting $end"). But if I add "then"s, i.e.,

    if i == 1 then print "one" elsif i == 2 then print "two" end

    then it works.

    Could someone explain just what newlines do in cases like these, or at
    least provide some guidelines to when I need them?
     
    Gary McGath, Oct 8, 2012
    #1
    1. Advertisements

  2. There's also ";" which can be used instead:

    $ ruby19 -ce 'if i == 1 then print "one" elsif i == 2 then print "two" end'
    Syntax OK
    $ ruby19 -ce 'if i == 1; print "one" elsif i == 2 then print "two" end'
    Syntax OK
    $ ruby19 -ce 'if i == 1 print "one" elsif i == 2 then print "two" end'
    -e:1: syntax error, unexpected tIDENTIFIER, expecting keyword_then or
    ';' or '\n'
    if i == 1 print "one" elsif i == 2 then print "two" end
    ^
    -e:1: syntax error, unexpected keyword_elsif, expecting $end
    if i == 1 print "one" elsif i == 2 then print "two" end
    ^

    Note: all on 1 line each.
    Basically you need to separate individual statements. You can do that
    with a line terminator or with semicolon. "end" does not need a
    separator before it because it is a keyword - unless you want to define
    a class:

    $ ruby19 -ce 'class X end'
    -e:1: syntax error, unexpected keyword_end, expecting '<' or ';' or '\n'
    -e:1: syntax error, unexpected $end
    $ ruby19 -ce 'class X; end'
    Syntax OK
    $

    I'm afraid I do not have more advice here. But this was really never a
    big issue for me IIRC. Just code away and let the syntax check give
    your the feedback. :)

    Kind regards

    robert
     
    Robert Klemme, Oct 8, 2012
    #2
    1. Advertisements

  3. Gary McGath

    Gary McGath Guest

    I see. Semicolon is a statement terminator, rather than being an
    assignment terminator as in C-family languages. Useful to know.
    Making conservative assumptions rather than trying to pare the code to
    the minimum number of characters also sounds like good advice (i.e.,
    Ruby is not Perl :).

    I've been using _Learning Ruby_ from O'Reilly, which is readable but not
    nearly as precise as I'd like. Thanks for the input.
     
    Gary McGath, Oct 8, 2012
    #3
  4. And that's good - definitively!
    Well, actually Ruby's syntax is quite flexible - maybe too flexible in
    some places. But it's great that you can omit brackets for methods
    arguments. That makes creating DSLs pretty easy. Actually some things
    which look like keywords are actually methods:

    irb(main):001:0> Class.instance_method :attr_reader
    => #<UnboundMethod: Class(Module)#attr_reader>
    irb(main):002:0> class Foo; method :attr_reader; end
    => #<Method: Class(Module)#attr_reader>

    Kind regards

    robert
     
    Robert Klemme, Oct 9, 2012
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.