[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

What is the reason for this syntax?

Kevin Olemoh

10/4/2006 7:39:00 PM

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

Quite frankly I find the second form to be more difficult to read
especially if one tends to create blocks with braces rather than the do
end keywords like I do. Is there some specific reason that both forms
are not supported by Ruby? It is needlessly restrictive with respect to
formatting in my opinion, perhaps a kind ruby-core developer could sneak
this syntax change into a future release?

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? :)


77 Answers

rajeev.networld

10/4/2006 8:01:00 PM

0

Kevin:

1. That is how it works and for me syntax #2 looks more easier.

2. elsif is same one we use in perl.

Ruby has more perl in it than python. It seems like you were a python
programmer that's why. I am perl programmer that this is natural to me.


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
>
> Quite frankly I find the second form to be more difficult to read
> especially if one tends to create blocks with braces rather than the do
> end keywords like I do. Is there some specific reason that both forms
> are not supported by Ruby? It is needlessly restrictive with respect to
> formatting in my opinion, perhaps a kind ruby-core developer could sneak
> this syntax change into a future release?
>
> 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? :)

Marcin Mielzynski

10/4/2006 8:07:00 PM

0

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

In ruby statements are terminated by newlines and block is a parameter
to the each method here, consider:

def meth *args
end

meth
1,2

what is Ruby supposed to do in such situation ? In your case you are
calling the each method without any argument and supplying another
statement:

do
end

which obviously is wrong

On the other hand Ruby allows breaking statements when situation is more
obvious:

def meth *args

end

meth 1,
2


>
> Quite frankly I find the second form to be more difficult to read
> especially if one tends to create blocks with braces rather than the do
> end keywords like I do. Is there some specific reason that both forms
> are not supported by Ruby? It is needlessly restrictive with respect to
> formatting in my opinion, perhaps a kind ruby-core developer could sneak
> this syntax change into a future release?
>

Nope, they are not equivalent. The {} form binds tighter than do end.

def meth,arg
yield
end

and:

meth a do

end

block applied to the meth method


meth a {

}

block applied do a (which could be a method call) and the result of a
block would be passed to the meth method


lopex

vidar

10/4/2006 8:09:00 PM

0


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
>

In Ruby an expression continues on the next line if syntax
unambiguously shows that the expression is incomplete.

In this case "a.each" is a complete expression by itself (a method call
with no arguments and no block), and so the "do" on the following line
occurs without anything to take the block.

Vidar

Gary Wright

10/4/2006 8:25:00 PM

0


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

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.

Gary Wright




Just Another Victim of the Ambient Morality

10/4/2006 11:38:00 PM

0

Just to add to a very good response to the original post...


<gwtmp01@mac.com> 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...



David Vallner

10/5/2006 11:20:00 PM

0

Kevin Olemoh wrote:
> 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.

Not in a semicolon-free language, I'm afraid. And if nothing else, this
reinforces use of a common style convention, which is a Good Thing.

Most of the people willing to fight vocally for the freedom to ignore
indentation and code style guidelines I've met never used CVS to see
just how horribly two people that have different autoformatting set in
their editors / IDEs confuse diff...

David Vallner

Charles Oliver Nutter

10/5/2006 11:55:00 PM

0

David Vallner wrote:
> Not in a semicolon-free language, I'm afraid. And if nothing else, this
> reinforces use of a common style convention, which is a Good Thing.

Well let's not go too far down that path, now, or we might add something
horrid like syntactic indentation...

--
Charles Oliver Nutter, JRuby Core Developer
headius@headius.com -- charles.o.nutter@sun.com
Blogging at headius.blogspot.com

David Vallner

10/6/2006 12:36:00 AM

0

Charles Oliver Nutter wrote:
> Well let's not go too far down that path, now, or we might add something
> horrid like syntactic indentation...
>

I like Python syntax, actually. It just takes a while for your brain to
stop breaking at the thought of pressing backspace meaning "end block" ;)

David Vallner

Louis J Scoras

10/6/2006 1:22:00 AM

0

On 10/5/06, Charles Oliver Nutter <Charles.O.Nutter@sun.com> wrote:
> David Vallner wrote:
> > Not in a semicolon-free language, I'm afraid. And if nothing else, this
> > reinforces use of a common style convention, which is a Good Thing.
>
> Well let's not go too far down that path, now, or we might add something
> horrid like syntactic indentation...

Exactly what I was thinking. On the other hand, although I don't care
for Python, the whitespace thing doesn't bother me in Haskell. I
don't know why, but I get the feeling that it wouldn't be so bad in
Ruby code either, but it's just a guess.

To tell the truth, I do wish there was a visible statement seperator
though (semi-colon). I find this

some_long_variable name = long_expression_which_needs
+ to_break_across_lines;

method
.chaing
.is.more.noticiable;

to be more readable than:

some_long_variable name = long_expression_which_needs +
to_break_across_lines

method.
chaining.
stands.out.less

Also, a syntax for associating more than one block with a method would
be nice too.

Of course these are minor gripes. As it is often cited, ruby is a
beautiful language.


--
Lou.

Martin Coxall

10/6/2006 8:57:00 AM

0

On 10/6/06, David Vallner <david@vallner.net> wrote:
> Kevin Olemoh wrote:
> > 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.
>

So, use a '\' at the end of a line to indicate it continues on the
next. Not the end of the world.

Martin