Afficher un message
Vieux 12/03/2008, 16h19   #1
Fred Talpiot
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut elsunless: Compiles but does not run. Standard?

In Ruby:
1) 'if' can be followed by 'elsif'.
2) 'unless' can NOT be followed by 'elsif'. It gets a syntax error.
3) 'unless' can be followed by 'elsunless', but it does not run okay.
3a) 'if' can also be followed by 'elsunless', with the same dismal
results.
4) So what's the standard?
5) The following code:
-------------------
puts 'ruby version: ' + RUBY_VERSION + ', ' + RUBY_RELEASE_DATE + ' for
' + RUBY_PLATFORM

unless true
puts "wrong wrong"
elsunless false
puts 'weird, but logical'
elsunless true
puts 'truly strange'
else
puts 'weirder yet' # This is what pops out!
end

# and 'elsif' is a syntax error following 'unless'
-------------------
Produces the following on WinXP:

>ruby unless.rb

ruby version: 1.8.6, 2007-09-24 for i386-mswin32
weirder yet

And, on Linux:

$ ruby unless.rb
ruby version: 1.8.6, 2007-09-24 for x86_64-linux
weirder yet

We can be sure this behavior is not as intended. We cannot be sure
exactly what was intended.

The following rule is from the wiki reference page:
http://en.wikibooks.org/wiki/Ruby_Pr...uctures#unless

|The unless expression is treated exactly like a negated if
expression:
|
| if !expression # is equal to using
| unless expression

Obviously, that rule is not being followed. If it were, the 'elsif'
would
be permitted.

The interpreter has a bug. The documentation may also be wrong, hard to
know for sure...

name already taken
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
 
Page generated in 0,04841 seconds with 9 queries