From: Kevin Olemoh Date: 2006-10-06T05:33:17+09:00 Subject: Re: What is the reason for this syntax? --------------090804070007030001090305 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Just Another Victim of the Ambient Morality wrote: > Just to add to a very good response to the original post... > > > wrote in message > news:59496518-3925-4E74-9B6E-B45A8C3B7E02@mac.com... > >> On Oct 4, 2006, at 3:39 PM, Kevin Olemoh wrote: >> >> >>> Hello I have been using ruby off and on for a few months and I have >>> been having a great time with the language but a few things bother me >>> about the syntax of the language itself. The two glaring issues are: >>> >>> 1. The syntax errors generated by the following code: >>> >>> a.each >>> do >>> #stuff >>> end >>> >>> for reasons I do not understand ruby demands that, that line be written >>> as: >>> a.each do >>> #stuff >>> end >>> >> Blocks are part of the syntax of a method call (do/end or {}) >> Blocks are optional. >> Newlines terminate statements. >> >> The net result of those three things is that: >> >> a.each >> >> is considered a syntactically valid and complete statement. Leaving >> Ruby to try >> to interpret >> >> do >> #stuff >> end >> > > The following code: > > a.each > do > #stuff > end > > ...is the equivalent of to: > > a.each; # note the optional statement terminator... > do > #stuff > end > > > >> as the next statement, which fails. If Ruby executes 'a.each' (without >> the >> block) you'll get a runtime exception. It is correct syntax, but 'each' >> insists >> that it be called with a block. >> >> You could of course give a hint to the parser that you want to continue >> the statement: >> >> a.each \ >> do >> #stuff >> end >> >> but escaping the newline in this case doesn't improve the readability >> (IMHO). >> >> >>> 2. What is with the elseif syntax specifically why is it elsif instead >>> of elseif when ruby already has an else keyword? I can't count how >>> many times I got errors because I decided to type elseif instead of >>> elsif while doing something with an if statement. I can name at least >>> two popular languages that use elseif not to mention the fact that if >>> English is your first language you will probably spell out else without >>> even realizing it since that is the correct way to spell the word in >>> English. Yes I know its a minor thing but if no one voices their >>> gripes how do people know something might need a bit of tweaking? :) >>> >> I doubt a survey of languages would come up with any sort of consistency >> for the keyword in this case, so you are basically asking why doesn't >> Ruby use the same syntax for the particular languages that you are >> familiar with, which seems like a somewhat arbitrary expectation and one >> that could never be satisfied for everyone. >> >> I'm not a parsing/grammar expert, but I also suspect there is some >> benefit to keywords not being prefixes of other keywords so that 'else' >> and 'elseif' create more parsing issues than 'else' and 'elsif'. I'm >> sure someone else could elaborate on that thought. >> > > In fact, Ruby has deep Perl roots, which is why so much of its syntax > is so Perl-like (just note its regular expressions). Perl uses "elsif" > and, thus, so does Ruby. Personally, I don't like it either but what can > you do... > > > > > > Thank you all for your explanations of the syntax I pointed out. All I really want with respect to the blocks is the freedom to put the braces where I like without escape sequences of course. The elsif thing I can pretty much avoid with a case the majority of the time just as I do in VB. (Cases for t3h win!! :P) It could be worse though this could be Bash with elif and fi to close an if >:o --------------090804070007030001090305--