Recent Changes - Search:

CIS 700

Software

Tutorials


PmWiki

pmwiki.org

edit SideBar

Gotchas

Program:

x = 2
if x == 1
  puts 'one'
elseif x == 2
  puts 'two'
else
  puts 'many'
end

Prints out:

many

Explanation:

The misspelled word elseif (should be elsif) is taken to be a method call. Since it is never reached, Ruby does not give a missing function error.

This is where syntax coloring can be a big help. I found this error when I noticed that elseif was not the same color as if and else.


Program:

   1 def one
   2   two
   3 end
   4
   5 one   
   6
   7 def two
   8   puts "success!"
   9 end
  10
  11 one

Problem:

Line 5 results in undefined local variable or method `two' for main:Object but there is no problem with lines 2 or 11.

Explanation:

At line 2, Ruby assumes there will be a method two. It's defined by the time one is called in line 11, but when one is called from line 5, two hasn't yet been defined.

Edit - History - Print - Recent Changes - Search
Page last modified on June 30, 2006, at 05:27 PM