Query regarding elsif

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”) }

Hmm. don’t know much about the possibility of adding the
keyword elseif, but you may want to try ‘-w’ to ruby.

I changed the first line to #!/usr/bin/env ruby -w

to get:

else.rb:13: warning: elseif (…) interpreted as method call
else.rb:11: warning: useless use of / in void context
0: 1
1: 1
2: 1
3: 1
4: 1
5: 1
6: 1
7: 1
8: 1
9: 1

Jim

···

On Mon, Aug 19, 2002 at 07:24:38AM +0900, Paul J. Sanchez wrote:

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?


Jim Freeze
If only I had something clever to say for my comment…
~

Possibly not the solution you were looking for, but if you use an editor
with syntax highlighting, you will receive immediate visual feedback
while typing the ‘elseif’ (i.e. it won’t turn bright yellow or
whatever). This usually catches me with this typo.

-kyle

···

On Mon, Aug 19, 2002 at 07:24:38AM +0900, Paul J. Sanchez wrote:

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?


http://mas.cs.umass.edu/~rawlins

rather like a fairy land, isn’t it…

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 add synonyms, and not break existing code.

Or am I making a mountain out of a mole hill?

I think it’s a valid point. I’ve tripped up on it myself.
Shell: elif. Tcl: elseif. Python: dunno.
Perl: elsif. Oooohhhh. I see. :slight_smile:

  1. Go to your Ruby source directory.

  2. Add new entries to the ‘keywords’ file

    elseif, kELSIF, kELSIF, EXPR_BEG
    elif, kELSIF, kELSIF, EXPR_BEG

  3. Regenerate the hashed lookup table (see comments at the top of ‘lex.c’):

    gperf -p -j1 -i 1 -g -o -t -N rb_reserved_word -k’1,3,$’ ./keywords >lex.c

  4. Add code to parse.y (nothing to do in this case)
    (Optional: generate warning/usage message for synonyms?)

  5. Re-make ‘ruby’

    make

  6. Test

a = 3
if a == 1
puts 'one’
elseif a == 2
puts 'two’
elif a == 3
puts 'three’
end

‘three’

OK, I’ve polluted this group enough for this week.
Take care!

···

On Mon, Aug 19, 2002 at 07:24:38AM +0900, Paul J. Sanchez wrote:

Mike Hall
http://www.enteract.com/~mghall

By the time he’s done all that, do you think he’ll have learned the correct
Ruby grammar?

Gavin

···

----- Original Message -----
From: “Mike Hall” mghall@enteract.com

  1. Go to your Ruby source directory.

  2. Add new entries to the ‘keywords’ file

    elseif, kELSIF, kELSIF, EXPR_BEG
    elif, kELSIF, kELSIF, EXPR_BEG

  3. Regenerate the hashed lookup table (see comments at the top of ‘lex.c’):

    gperf -p -j1 -i 1 -g -o -t -N rb_reserved_word -k’1,3,$’ ./keywords
    lex.c

  4. Add code to parse.y (nothing to do in this case)
    (Optional: generate warning/usage message for synonyms?)

  5. Re-make ‘ruby’

    make

  6. Test

a = 3
if a == 1
puts 'one’
elseif a == 2
puts 'two’
elif a == 3
puts 'three’
end

‘three’

OK, I’ve polluted this group enough for this week.
Take care!

Mike Hall
http://www.enteract.com/~mghall