[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

so surprised syntax error not caught here

SpringFlowers AutumnMoon

9/25/2007 4:50:00 PM


the following syntax error for "elif" is not caught? (should be elsif)

def foo(i)
if i < 0
return "Less than 0"
elif i == 0
return "it is zero"
else
return "Greater than 0"
end
end

p foo(-2)
p foo(0)
p foo(10)

==================
Output:

"Less than 0"
"Greater than 0"
"Greater than 0"
--
Posted via http://www.ruby-....

3 Answers

Austin Ziegler

9/25/2007 4:59:00 PM

0

On 9/25/07, SpringFlowers AutumnMoon <summercoolness@gmail.com> wrote:
> the following syntax error for "elif" is not caught? (should be elsif)
>
> def foo(i)
> if i < 0
> return "Less than 0"
> elif i == 0
> return "it is zero"
> else
> return "Greater than 0"
> end
> end
>
> p foo(-2)
> p foo(0)
> p foo(10)

It's treating "elif" as a (possible) method call that's never reached
because of the "return". You don't need "return" for what you have:

def foo(i)
if i < 0
"Less than 0"
elif i.zero? # i == 0
"Equal to 0"
else
"Greater than 0"
end
end

That will fail, but not with a syntax error. (It should fail with an
UnknownMethod.) Ruby is expression-oriented, and the last expression
executed in the execution path is returned from the execution path.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Phlip

9/25/2007 5:05:00 PM

0

SpringFlowers AutumnMoon wrote:

> the following syntax error for "elif" is not caught? (should be elsif)
>
> def foo(i)
> if i < 0
> return "Less than 0"
> elif i == 0
> return "it is zero"

Ruby is interpretive; it does not link all its symbols to real
entities at compile time. (Unlike certain other languages we could
mention.

elif could be a function that takes a boolean. Ruby can't know this
until it interprets the line, because you might have used eval() or
something to create that function before getting here.

In exchange for a lot more flexibility, Ruby does fewer sanity checks
at parse time.

To keep Ruby on track, and catch errors like this, all Ruby developers
should write unit tests for everything.

--
Phlip

Terry Poulin

9/26/2007 10:37:00 PM

0

Austin Ziegler wrote:
>
> It's treating "elif" as a (possible) method call that's never reached
> because of the "return". You don't need "return" for what you have:
>
> def foo(i)
> if i < 0
> "Less than 0"
> elif i.zero? # i == 0
> "Equal to 0"
> else
> "Greater than 0"
> end
> end
>
> That will fail, but not with a syntax error. (It should fail with an
> UnknownMethod.) Ruby is expression-oriented, and the last expression
> executed in the execution path is returned from the execution path.
>
> -austin
> --
> Austin Ziegler * halostatue@gmail.com * http://www.halo...
> * austin@halostatue.ca * http://www.halo...feed/
> * austin@zieglers.ca
>
>


My word, you have just given me a reason to stop using returns !!!


It doesn't hurt code readability to go by syntax rather then insert the "Yes
Mr/Miss Programmer don't forget we return here" return statements through out
code ether.


TerryP.


--

Email and shopping with the feelgood factor!
55% of income to good causes. http://www.ip...