What is the reason for this syntax?

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

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

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

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

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

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

···

On Oct 4, 2006, at 3:39 PM, Kevin Olemoh wrote:

I confess I haven't really followed this thread carefully, but--I
totally agree on the 'elsif' keyword. Wouldn't it be possible for ruby
to accept *either* spelling?

If so, I would definitely advocate it--you don't lose anything for the
perlsters, and those of us coming from other languages won't have to
deal w/the IMHO surprising spelling.

I remember dealing w/this same issue while learning pl/sql. After
staring at my modest program for like 40 minutes looking for the
complained-of syntax error, I was bitter bitter bitter when I finally
learned that I was expected to mis-spell 'else'...

I actually thought ruby did this already--had to write a little script
to verify that it doesn't...

Cheers,

-Roy

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

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

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

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

···

On Oct 4, 2006, at 3:39 PM, Kevin Olemoh wrote:

Vidar Hokstad wrote:

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

So why can't the parser continue a statement on to the next line if the syntax unambiguously shows that the expression on the next line is the continuation of the line before it, such as lines starting with do or .foo which are incorrect otherwise?

Hi --

···

On Sun, 15 Oct 2006, rpardee@gmail.com wrote:

I confess I haven't really followed this thread carefully, but--I
totally agree on the 'elsif' keyword. Wouldn't it be possible for ruby
to accept *either* spelling?

If so, I would definitely advocate it--you don't lose anything for the
perlsters, and those of us coming from other languages won't have to
deal w/the IMHO surprising spelling.

I remember dealing w/this same issue while learning pl/sql. After
staring at my modest program for like 40 minutes looking for the
complained-of syntax error, I was bitter bitter bitter when I finally
learned that I was expected to mis-spell 'else'...

I actually thought ruby did this already--had to write a little script
to verify that it doesn't...

When you're using Ruby, it's best just to come from Ruby. Then you
don't have to be surprised by things like this, or feel any resentment
toward Ruby for failing to be C or Java or whatever. Languages do
lots of things differently from each other. Some day they may all
converge, but that convergences doesn't have to be called Ruby :slight_smile:

David

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

Just Another Victim of the Ambient Morality wrote:

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

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

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

···

On Oct 4, 2006, at 3:39 PM, Kevin Olemoh wrote:

It might require arbitrary parsing lookahead or other facilitation at
the parser/lexer level.

In any case, I just suggest getting over it (pun intended) and falling
in line (muhahaa) with the normal Ruby layout :slight_smile:

···

On 2006.10.07 00:50, John Turner wrote:

Vidar Hokstad wrote:
>In Ruby an expression continues on the next line if syntax
>unambiguously shows that the expression is incomplete.

So why can't the parser continue a statement on to the next line if the
syntax unambiguously shows that the expression on the next line is the
continuation of the line before it, such as lines starting with do or
.foo which are incorrect otherwise?

dblack@wobblini.net wrote:

Languages do
lots of things differently from each other. Some day they may all
converge, but that convergences doesn't have to be called Ruby :slight_smile:

Nor does the converged spoken language have to be called English. :slight_smile:

But seriously, folks, a billion or so people speak Chinese and I've
forgotten how many speak Spanish and Hindi. Is English even number three
as a native spoken language yet?

Of course, I view almost all contemporary programming languages as
dialects of C, since most of the interpreters and compilers are either
written in C or bootstrapped from C.

<ducking>

But isn't almost everybody coming from *somewhere*? This seems to me a
cost-free change--a win-win...

dblack@wobblini.net wrote:

···

Hi --

On Sun, 15 Oct 2006, rpardee@gmail.com wrote:

> I confess I haven't really followed this thread carefully, but--I
> totally agree on the 'elsif' keyword. Wouldn't it be possible for ruby
> to accept *either* spelling?
>
> If so, I would definitely advocate it--you don't lose anything for the
> perlsters, and those of us coming from other languages won't have to
> deal w/the IMHO surprising spelling.
>
> I remember dealing w/this same issue while learning pl/sql. After
> staring at my modest program for like 40 minutes looking for the
> complained-of syntax error, I was bitter bitter bitter when I finally
> learned that I was expected to mis-spell 'else'...
>
> I actually thought ruby did this already--had to write a little script
> to verify that it doesn't...

When you're using Ruby, it's best just to come from Ruby. Then you
don't have to be surprised by things like this, or feel any resentment
toward Ruby for failing to be C or Java or whatever. Languages do
lots of things differently from each other. Some day they may all
converge, but that convergences doesn't have to be called Ruby :slight_smile:

David

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

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

No change is cost free, and this is a lose-lose proposition.

You're complaining about:

  if foo
    do_foo
  elsif bar
    do_bar
  end

What does this look like in other languages?

  if (foo)
  {
    do_foo();
  }
  else if (bar)
  {
    do_bar();
  }

  if foo then
    do_foo;
  elseif bar then
    do_bar;
  end;

  if [ ! -z "$foo" ]; then
    do_foo
  elif [ !-z "$bar" ]; then
    do_bar
  fi

Which of these would you have Ruby emulate? All of them? If you're
wanting a PHP-ism, that's a damned good reason to *not* have it (IMO,
PHP has done *everything* wrong that's possible to do wrong in language
design). Why can't I have my shell-ism (elif), if you get your PHP-ism?

Sorry, but Matz made a choice here. If you don't like the choice, you
can either (1) create a preprocessor that converts your preferred form
into the correct form, (2) make an autocorrection in your favourite
editor to turn elseif into elsif as Ruby requires, (3) use a different
language, or (4) get used to it, already.

I've been using Ruby for four years; there are very few warts on the
language, and this ain't one of them.

-austin

···

On 10/14/06, rpardee@gmail.com <rpardee@gmail.com> wrote:

But isn't almost everybody coming from *somewhere*? This seems to me a
cost-free change--a win-win...

--
Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/
               * austin@halostatue.ca * You are in a maze of twisty little passages, all alike. // halo • statue
               * austin@zieglers.ca

Hi --

Hi --

I confess I haven't really followed this thread carefully, but--I
totally agree on the 'elsif' keyword. Wouldn't it be possible for ruby
to accept *either* spelling?

If so, I would definitely advocate it--you don't lose anything for the
perlsters, and those of us coming from other languages won't have to
deal w/the IMHO surprising spelling.

I remember dealing w/this same issue while learning pl/sql. After
staring at my modest program for like 40 minutes looking for the
complained-of syntax error, I was bitter bitter bitter when I finally
learned that I was expected to mis-spell 'else'...

I actually thought ruby did this already--had to write a little script
to verify that it doesn't...

When you're using Ruby, it's best just to come from Ruby. Then you
don't have to be surprised by things like this, or feel any resentment
toward Ruby for failing to be C or Java or whatever. Languages do
lots of things differently from each other. Some day they may all
converge, but that convergences doesn't have to be called Ruby :slight_smile:

But isn't almost everybody coming from *somewhere*? This seems to me a
cost-free change--a win-win...

But then if a Rubyist starts learning Java, Java has to change to make
the Rubyist happy. And C has to change to make Haskell programmers
happy. And Python has to change to make Smalltalk programmers happy.
And so on.

It seems to me it's best to break the cycle. Of course languages
evolve from, and borrow from, each other. But they don't incur an
obligation to add features and behaviors simply because practitioners
of other languages are used to those features and behaviors. It's
better just to get used to the language you're using, when you're
using it.

David

···

On Sun, 15 Oct 2006, rpardee@gmail.com wrote:

dblack@wobblini.net wrote:

On Sun, 15 Oct 2006, rpardee@gmail.com wrote:

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

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

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

Martin

···

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.

Personally I don't think else if needs to be an actual reserved word.
It would make more sense just to allow people to put an if right after
an else (with a space or newline, not some silly terminator.) in order
to achieve the effect of the keyword. I am not entirely certain of
this but I think that this is how C++ and java happen to work.

···

On 10/15/06, dblack@wobblini.net <dblack@wobblini.net> wrote:

Hi --

On Sun, 15 Oct 2006, rpardee@gmail.com wrote:

> dblack@wobblini.net wrote:
>> Hi --
>>
>> On Sun, 15 Oct 2006, rpardee@gmail.com wrote:
>>
>>> I confess I haven't really followed this thread carefully, but--I
>>> totally agree on the 'elsif' keyword. Wouldn't it be possible for ruby
>>> to accept *either* spelling?
>>>
>>> If so, I would definitely advocate it--you don't lose anything for the
>>> perlsters, and those of us coming from other languages won't have to
>>> deal w/the IMHO surprising spelling.
>>>
>>> I remember dealing w/this same issue while learning pl/sql. After
>>> staring at my modest program for like 40 minutes looking for the
>>> complained-of syntax error, I was bitter bitter bitter when I finally
>>> learned that I was expected to mis-spell 'else'...
>>>
>>> I actually thought ruby did this already--had to write a little script
>>> to verify that it doesn't...
>>
>> When you're using Ruby, it's best just to come from Ruby. Then you
>> don't have to be surprised by things like this, or feel any resentment
>> toward Ruby for failing to be C or Java or whatever. Languages do
>> lots of things differently from each other. Some day they may all
>> converge, but that convergences doesn't have to be called Ruby :slight_smile:
>>
> But isn't almost everybody coming from *somewhere*? This seems to me a
> cost-free change--a win-win...

But then if a Rubyist starts learning Java, Java has to change to make
the Rubyist happy. And C has to change to make Haskell programmers
happy. And Python has to change to make Smalltalk programmers happy.
And so on.

It seems to me it's best to break the cycle. Of course languages
evolve from, and borrow from, each other. But they don't incur an
obligation to add features and behaviors simply because practitioners
of other languages are used to those features and behaviors. It's
better just to get used to the language you're using, when you're
using it.

David

--
                   David A. Black | dblack@wobblini.net
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

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" :wink:

David Vallner