Why is this not a syntax error?

Switching back and forth between Python and Ruby bit me in the butt today. For example, consider the following Ruby code:

   a = 1

   if a == 0
       puts 'zero'
   elif a == 1
       puts 'one'
   else
       puts 'not one'
   end

The code in error is the "elif" statement. "elif" is python's else-if statement, as opposed to Ruby's elsif statement.

Now, I would expect the above Ruby code to generate a syntax error stating that "elif" is bad. However, when I run the above code, instead of a syntax error, the code executes and returns "not one", so it is not erroring out on the "elif" and it is just falling through to the else statement.

Why is this not a syntax error?

I googled for "Ruby elif" but did not find anything relevant.

Jamey Cribbs

Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.

   if a == 0
       puts 'zero'
   elif a == 1
       puts 'one'
   else
       puts 'not one'
   end

You have just written

      if a == 0
         puts 'zero'
         elif(a == 1) # call the method elif
         puts 'one'
      else
         puts 'not one'
      end

run it with a = 0, to see the error message

Guy Decoux

Hi,

Switching back and forth between Python and Ruby bit me in the butt
today. For example, consider the following Ruby code:

  a = 1

  if a == 0
      puts 'zero'
  elif a == 1
      puts 'one'
  else
      puts 'not one'
  end

It's seen as

   if a == 0
       puts 'zero'
       elif(a == 1)
       puts 'one'
   else
       puts 'not one'
   end

See?

              matz.

···

In message "Re: Why is this not a syntax error?" on Sat, 24 Sep 2005 00:31:23 +0900, Jamey Cribbs <cribbsj@oakwood.org> writes:

Jamey Cribbs wrote:

Switching back and forth between Python and Ruby bit me in the butt
today. For example, consider the following Ruby code:

  a = 1

  if a == 0
      puts 'zero'
  elif a == 1
      puts 'one'
  else
      puts 'not one'
  end

Syntax hilighting in your editor works wonders on this kind of butt-bite.

···

--
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

ts wrote:

"J" == Jamey Cribbs <cribbsj@oakwood.org> writes:
           
   if a == 0
       puts 'zero'
   elif a == 1
       puts 'one'
   else
       puts 'not one'
   end

You have just written

     if a == 0
        puts 'zero'
        elif(a == 1) # call the method elif
        puts 'one'
     else
        puts 'not one'
     end

run it with a = 0, to see the error message

Gotcha!

Thanks, Guy.

Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.

Joel VanderWerf <vjoel@path.berkeley.edu> writes:

Jamey Cribbs wrote:

Switching back and forth between Python and Ruby bit me in the butt
today. For example, consider the following Ruby code:

  a = 1

  if a == 0
      puts 'zero'
  elif a == 1
      puts 'one'
  else
      puts 'not one'
  end

Syntax hilighting in your editor works wonders on this kind of butt-bite.

It's enough if they properly indent your code. :slight_smile:

···

Joel VanderWerf

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org