I am new to Ruby and recently ran into a problem comparing with values when creating a Ruby on Rails application. In the controller, I had the following statement, which always returned false:
if (user.id != params[:id])
The problem was that user.id (which is the active record) is an integer, and params [: id] is a string. It took me a while to figure this out, and I finally changed it to:
if (user.id != params[:id].to_i)
Now the instruction works as expected.
To avoid this error in the future, is there a way to “compile” or get Ruby to warn you if you try to compare 2 different types? Some other problems that I encountered, I would like to “compile check”:
- Warn me if I create a variable but do not use it. To help check for typos in variable names.
- Make sure that the method exists in the class, so I can avoid typos in the method name and also help in refactoring, for example, if I rename the method.
I am currently using Ruby 1.8.6-27 RC2 with Rails 2.3.2 and RadRails IDE on Windows.
ruby ruby-on-rails compilation syntax-checking
Chris c
source share