Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.ruby Subject: Re: Ruby newbie newline question Date: Mon, 08 Oct 2012 22:44:15 +0200 Lines: 65 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net VQ3P9D48xJsT2/byE6mFXQRWdEMPDVDLXLcyEz11yAo82ubgQ= Cancel-Lock: sha1:IpbrMfZ5e99m+J3T0d5JCEuAAKg= User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:15.0) Gecko/20120907 Thunderbird/15.0.1 In-Reply-To: Xref: csiph.com comp.lang.ruby:6638 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/