I am still going through the Rails Tutorial. I am starting to have some issues with Ruby. It seems like there are methods in files that just float in space and land in your class.
For example, in the tutorial we make a class that inherits from ActiveRecord:: Base. It uses a method called “validates”, that as far as I can tell is part of ActiveModel:: Validations:: ClassMethods. I figured out that the “Methods” frame at the Ruby On Rails site has all the methods for all the classes and modules. Which is good, because the User class does not mention the ClassMethods module. I cannot figure out from the documentation that an ActiveRecord:: Base class can see/use the methods in ActiveModel:: Validations:: ClassMethods.
I posted this on LinkedIn, and I got a couple of responses. One guy’s answer involved looking through the source code, and seeing what classes are visible via the $LOAD_PATH variable. Granted, I posted that I looked through the source code, but I would prefer some way to know what methods are available without diving through the source code. I would like to get the answer via the docs. Another answer said that “the ActiveModel module is part of the ActiveRecord module”. Once again, I cannot find anything in the docs that tells me that. This second person referred me to the “Programming Ruby” book. Perhaps that page has the answer (I will read it later).
Another example is the tests use a method called “have_selector”, which from Googling I conclude is part of the Webrat gem. The test files have “require ‘spec_helper'” in them, and there is a spec/spec_helper.rb file in the project. But none of the test files say anything like “require webrat”.
In Java, every method must be part of your class, inherited from a parent, or part of a class that you explicitly import. It seems like in Ruby imports can be “chained” (for lack of a better term). Perhaps the spec_helper.rb file starts a chain that at some point refers to a file in the webrat gem. But I would prefer that every file that uses “have_selector” say that it is using webrat, not something that calls webrat somewhere else.
Perhaps I should have gone through a Ruby tutorial before trying a Rails tutorial. Sometimes I like to know something more about a method, but I am sometimes having a hard time finding out where the method is from. Right now I feel like I have to go down a rabbit hole to find something.
Image from Wikipedia.