Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby newbie newline question

Reply
Thread Tools

Ruby newbie newline question

 
 
Gary McGath
Guest
Posts: n/a
 
      10-08-2012
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 http://www.mcgath.com
 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      10-08-2012
On 08.10.2012 20:18, Gary McGath wrote:
> 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.


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.

> Could someone explain just what newlines do in cases like these, or at
> least provide some guidelines to when I need them?


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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
 
 
 
Gary McGath
Guest
Posts: n/a
 
      10-08-2012
On 10/8/12 4:44 PM, Robert Klemme wrote:

> 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
> ^


I see. Semicolon is a statement terminator, rather than being an
assignment terminator as in C-family languages. Useful to know.

> 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.


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 http://www.mcgath.com
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-09-2012
On 10/08/2012 11:57 PM, Gary McGath wrote:
> On 10/8/12 4:44 PM, Robert Klemme wrote:
>
>> 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
>> ^

>
> I see. Semicolon is a statement terminator, rather than being an
> assignment terminator as in C-family languages. Useful to know.
>
>> 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.

>
> 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 .


And that's good - definitively!

> 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.


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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: How to avoid newline after template processing? J Bondo XML 7 04-25-2009 05:22 PM
#!/usr/bin/ruby , #!/usr/bin/ruby -w , #!/usr/bin/ruby -T?, #!/usr/bin/ruby -T1... anne001 Ruby 1 04-23-2006 03:02 PM
Regular expression that doesn't recognize newline Antonio ASP .Net 0 01-19-2005 09:23 AM
string.split and Environment.NewLine question. =?Utf-8?B?QXNoYQ==?= ASP .Net 1 12-30-2004 09:33 AM
expression eats newline Gerard Oberle Perl 3 08-01-2003 05:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57