Syntax Checker or Compilation of a Ruby on Rails Application

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.

+6
ruby ruby-on-rails compilation syntax-checking
source share
6 answers

The best solution I found was an IDE that performed parsing on the fly, such as RubyMine. I'm not sure if it solved my original problem, but it helped me find and fix several other syntaxes and error compilation. Thank you all for your suggestions.

0
source share

Check first, then code. If you write tests that cover all the branches of your application, you get confidence that your code is working and gives the correct results.

EDIT: I must point out that the ability to compare two types, independent of method names until the last second, etc., are the main functions of Ruby.

You do not call the method the way you send a message to an object. Then the object is responsible for determining the method of processing this method. In Rails, this is used to access DB columns in ActiveRecord. There are no methods for columns until a message with a column name is sent to the object.

Static typing in Ruby goes against the duck-typing system. You can often get polymorphism for free without worrying about complex inheritance / interface schemes.

I propose to cover these functions and compensate for uncertainty through testing.

+5
source share

Ruby does not allow overriding the == operator for an object. In ruby ​​1.8 you cannot, Ruby 1.9 should have done, but I could not get my script to work for the main classes. It works well for custom objects.

 class Object alias :equal_without_warning :== def ==(object) unless self.class == object.class warn("Comparing `#{self.class}' with `#{object.class}'") end equal_without_warning(object) end end 

Assuming I did not make some kind of silly coding mistake, the answer is NO: you cannot check if you are comparing objects of different types.

In addition, I would say no. In fact, Ruby is not designed to work this way, it is more a java approach than a Ruby style.

+2
source share

Ruby should not be safe. It allows you to compare any two objects and where it comes from most of its energy. Rails would not have been possible without such a dynamic design.

Even a compiled language such as Java or C will not stop you from running == on two objects. As Ben said, it's best to test first. Inspect the structures you are working with. One way to get information about a Ruby object is to use:

 puts object.class 
+1
source share

In general, the best way (I know) to avoid this type of problem for dynamic / scripting languages ​​is to translate the “logic” into methods / commands and write unit tests for them. Basically, anything that can fail should be checked. The code on the page should be a dumb logic ... and not display only those elements that meet certain criteria, it should display all the elements and get this list of elements from a method that returns only those that should be displayed.

+1
source share

Two things I suggest:

One: read on IRB (or script / console for rails). A common development practice in dynamic languages ​​is to try pieces of code inside a "live" interpreter (for example, an IRB or a rail console). This practice goes back to the earliest dynamic languages ​​such as Smalltalk and Lisp. Ruby-debug is also very useful for troubleshooting and would be a very simple way to figure out the error in your example.

Two: read Duck Seal. Types and variables work a little differently in Ruby than many people expect them to. As I understand it, a variable of type user.id has no type. The value points to user.id, has a type, but the variable itself does not. I believe that part of why there is no tool that would tell you that your mistake was before the program started. Comparing these two variables is not an error because the variables are not of type. user.id pointed to an integer at this point in your program, but it would be completely legal to assign user.id to point to a string, and at that point this comparison would make more sense. :-)

0
source share

All Articles