Re: elsunless: Compiles but does not run. Standard?
2008/3/12, Fred Talpiot <fredistic@gmail.com>:
> [...]
> 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
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
|