Forum: Ruby Line breaking in Ruby can be dangerous

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cee38055ae36590c654c04c2d5cc2778?d=identicon&s=25 Sky Yin (cookoo)
on 2006-01-23 13:42
(Received via mailing list)
After programing in ruby for the past 3 months, I must say I'm
happier. However, I find where to break a long expression into lines
can be potentially dangerous and very hidden to debug. For example:

>>begin
?>  (1 * 2
?>  + 1)
?> end
=> 1

I guess this may be attributed to the Ruby convention that the return
value by default is from the last line. The weird thing is that even
parentheses can't guarantee the result to be expected.

I'm still using 1.8.2 on win32. Is it a bug of that version?
D84df7c68f790e492c4ad4ec5fe65547?d=identicon&s=25 Florian Frank (Guest)
on 2006-01-23 14:15
(Received via mailing list)
On 2006-01-23 21:39:33 +0900, Sky Yin wrote:
> >>begin
> ?>  (1 * 2
> ?>  + 1)
> ?> end
> => 1
>
> I guess this may be attributed to the Ruby convention that the return
> value by default is from the last line. The weird thing is that even
> parentheses can't guarantee the result to be expected.

"1 * 2" and "+ 1" are both valid ruby expressions, the latter "(unary +)
1". How should Ruby find out, that you wanted to continue your first
expression in the next line?

If you want to avoid this, only break lines after operators, ",", etc.
like

1 * 2 +
  1

You can also use

1 * 2\
  + 1

but I try to avoid this way, I think it's kind of ugly.
9a1a4c7f4da6961ef3f6503d7ff33a53?d=identicon&s=25 Adriano Ferreira (Guest)
on 2006-01-23 14:46
(Received via mailing list)
On 1/23/06, Florian Frank <flori@nixe.ping.de> wrote:
> "1 * 2" and "+ 1" are both valid ruby expressions, the latter "(unary +)
> 1". How should Ruby find out, that you wanted to continue your first
> expression in the next line?

Ruby would find it out because of the leading "(" or that would be
what I and apparently Sky Yin expect.

This works:
irb(main):001:0> (1
irb(main):002:1> )
=> 1

This too:
irb(main):003:0> begin (1
irb(main):004:2> ) end
=> 1

But I think I understand why it works this way. In Yin's example:

(1*2
+1)

is interpreted just like
(1*2;
+1)

what would not happen if he had written
(1*2+
1)

which gives a proper answer.

Kind regards,
Adriano.
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2006-01-23 15:04
(Received via mailing list)
Sky Yin <sky.yin@gmail.com> writes:

>>>begin
> ?>  (1 * 2
> ?>  + 1)
> ?> end
> => 1

Simply use:
  1 * 2 +
  1

or:

  1 * 2 \
  + 1

> I guess this may be attributed to the Ruby convention that the return
> value by default is from the last line. The weird thing is that even
> parentheses can't guarantee the result to be expected.

That's because you can have multiple statements in (), e.g.:

(puts "foo"
puts "bar")

#=> foo
#=> bar
Cee38055ae36590c654c04c2d5cc2778?d=identicon&s=25 Sky Yin (cookoo)
on 2006-01-23 15:19
(Received via mailing list)
Thanks. When I first found it out, it's really a *big* surprise. This
actually took me quite a while to debug, cause the long expression
itself that I wrote simply looked nothing-wrong at the first glance.
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2006-01-23 17:44
(Received via mailing list)
Sky Yin <sky.yin@gmail.com> writes:

> Thanks. When I first found it out, it's really a *big* surprise. This
> actually took me quite a while to debug, cause the long expression
> itself that I wrote simply looked nothing-wrong at the first glance.

I recommend an intelligent-indenting editor.
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 unknown (Guest)
on 2006-01-23 23:14
(Received via mailing list)
Quoting Christian Neukirchen <chneukirchen@gmail.com>:

> That's because you can have multiple statements in (), e.g.:
>
> (puts "foo"
> puts "bar")
>
> #=> foo
> #=> bar

Whoa.

-mental
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 unknown (Guest)
on 2006-01-23 23:44
(Received via mailing list)
Quoting Florian Frank <flori@nixe.ping.de>:

> "1 * 2" and "+ 1" are both valid ruby expressions, the latter
> "(unary +)
> 1". How should Ruby find out, that you wanted to continue your
> first expression in the next line?

The parenthesis.  It shouldn't parse if they were treated as
separate expressions.

-mental
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 unknown (Guest)
on 2006-01-24 00:02
(Received via mailing list)
Quoting mental@rydia.net:

> Quoting Florian Frank <flori@nixe.ping.de>:
>
> > "1 * 2" and "+ 1" are both valid ruby expressions, the latter
> > "(unary +)
> > 1". How should Ruby find out, that you wanted to continue your
> > first expression in the next line?
>
> The parenthesis.  It shouldn't parse if they were treated as
> separate expressions.

Okay, never mind.  Now I know the real answer.

(1 * 2
+ 1)

parses just like:

(1 * 2;+ 1)

-mental
8d6f5daee16e380ce0ac00395b417fb6?d=identicon&s=25 Daniel Schüle (Guest)
on 2006-01-24 17:46
(Received via mailing list)
>> I guess this may be attributed to the Ruby convention that the return
>> value by default is from the last line. The weird thing is that even
>> parentheses can't guarantee the result to be expected.
>
> "1 * 2" and "+ 1" are both valid ruby expressions, the latter "(unary +)
> 1". How should Ruby find out, that you wanted to continue your first
> expression in the next line?

because of (

I would also expect either a SyntaxError or correct result (3)

the latter seems more natural to me

Regards, Daniel
9a1a4c7f4da6961ef3f6503d7ff33a53?d=identicon&s=25 Adriano Ferreira (Guest)
on 2006-01-24 19:21
(Received via mailing list)
On 1/24/06, Daniel Schüle <uval@rz.uni-karlsruhe.de> wrote:
> because of (
>
> I would also expect either a SyntaxError or correct result (3)
>
> the latter seems more natural to me

A SyntaxError here may seem unnatural for Ruby syntax rules.
* Expressions ("1+2", "obj.foo()") are statements.
* Statements can be separated by ";" or line ends.
* Parenthesized expressions may contain assignments separated by line
ends.

So there is no ground for a syntax error in these cases.

Adriano.
This topic is locked and can not be replied to.