I'm a ruby newbie, but an old hand at programming (30 years, more than
a dozen languages). I'm constantly running afoul of "elsif" - as a
native English speaker, my fingers really want to type "elseif".
Usually the mistake shows up immediately, but not always. For
example, Ruby will parse the little test program given below without
generating an error message, but the output surprised me until I
tracked down my mistake. No error message is generated until you test
the power function with a negative exponent.
To me, that missing "e" is a human factors issue. I realize that
changing the syntax would break virtually all existing code, but would
it be possible to add "elseif" as an alternate syntax? Or am I making
a mountain out of a mole hill?
#!/usr/bin/env ruby
def square(n)
n * n
end
def power(base, n)
if (n == 0)
1
elsif (n < 0)
1.0 / power(base, -n)
# Change the following to "elsif" and it works
elseif (n & 1) # n is odd
base * power(base, n-1)
else # n is even
square(power(base, n/2))
end
end
# The following prints ten 1's!
10.times { |x| print(x, ": ", power(2,x), "\n") }