In Ruby:
- ‘if’ can be followed by ‘elsif’.
- ‘unless’ can NOT be followed by ‘elsif’. It gets a syntax error.
- ‘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.
- So what’s the standard?
- 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_Programming/Syntax/Control_Structures#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
Are you sure about this. I am getting:
fred.rb:4:in x': undefined method
elsunless’ for main:Object
(NoMethodError)
I think that there is no such thing as elsunless. How about some more
realistic test code?
Hi,
At Wed, 12 Mar 2008 23:19:49 +0900,
Fred T. wrote in [ruby-talk:294281]:
- ‘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.
No such keyword.
unless true
puts “wrong wrong”
elsunless false
This is assumed a method call, but not reached.
2008/3/12, Fred T. [email protected]:
else
puts ‘weirder yet’ # This is what pops out!
end
elsunless is not a keyword in Ruby. Let’s reformat your
code a little bit:
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
Syntax wise, Ruby sees the “elsunless” occurences here
as plain method calls. Since the unless condition is true,
the whole block gets never executed and there’s no exception.
Let’s compare in irb:
irb(main):001:0> elsif true
SyntaxError: compile error
(irb):1: syntax error, unexpected kELSIF
elsif true
^
from (irb):1
irb(main):002:0> elsunless true
NoMethodError: undefined method `elsunless' for main:Object
from (irb):2
Stefan
On Wed, Mar 12, 2008 at 3:19 PM, Fred T. [email protected]
wrote:
Ruby is an interpreted language, so it ignores your “unless true” part
and doesn’t parse what’s after it until the “else”. Your code should
look like that:
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
Nobu is right. Try “unless false” instead of “unless true” and you’ll
get a NameError
2008/3/12, Stefan L. [email protected]:
else
puts 'weirder yet' # This is what pops out!
end
Damn, the “else” should not be indented.
Stefan
2008/3/12, Fred T. [email protected]:
Yup, you guys were right about ONE thing, the ‘elsunless’ was
not being parsed. It ain’t a keyword. It ain’t nuthin’.
BUT the ‘unless … elsif’ sequence SHOULD be accepted, if the
wiki is right. The wiki claims that ‘unless X’ is the same as
‘if ! X’. And that is NOT true with regard to ‘elsif’.
The wiki first describes the plain if expression - without
else or elsif. Then it describes unless and says it’s the
opposite of if. And only afterwards comes the description
of else and elsif. Makes sense to me.
But it’s a wiki and you’re invited to improve it if you
think it needs clarification.
(Well, and I’d find it ugly if Ruby actually allowed and
elsif in an unless.)
Stefan
Yup, you guys were right about ONE thing, the ‘elsunless’ was
not being parsed. It ain’t a keyword. It ain’t nuthin’.
BUT the ‘unless … elsif’ sequence SHOULD be accepted, if the
wiki is right. The wiki claims that ‘unless X’ is the same as
‘if ! X’. And that is NOT true with regard to ‘elsif’.
name already taken
Fred T. wrote:
Yup, you guys were right about ONE thing, the ‘elsunless’ was
not being parsed. It ain’t a keyword. It ain’t nuthin’.
BUT the ‘unless … elsif’ sequence SHOULD be accepted, if the
wiki is right.
Why should we care about this wiki? There are thousands of ressources on
Ruby. If you want to fix them when they aren’t reliable enough, you are
welcomed to do so but I don’t see what you expect us to do with
imaginary elsunless and elsif keywords in the unless constructs.
Lionel
Lionel: I never meant you had to do anything, I was asking about the
standard.
Why the Wiki?
I went to http://www.ruby-lang.org/en/documentation/ and started looking
down the page for a ‘language reference’. The wiki is the only one
described on that page as a ‘reference’ to the language. The rest of
the references there are to the libraries.