If you find this convoluted, don’t feel discouraged. It is—but it’s convolutedfor the sake of clean organization. Instead of writing belongs_to and the otherassociation methods directly as class methods of ActiveRecord::Base, Rails putsthem in a module that clearly labels them with the role they’re going to play:ActiveRecord::Associations::ClassMethods. Then, ActiveRecord::Base is extendedwith that module, at which point things proceed as if that module’sinstance methods were class methods of ActiveRecord::Base the whole time. Thebest of both worlds is preserved: The code remains organized and labeled withmeaningful class and module names, while the programmer can do things like:class Edition < ActiveRecord::Basebelongs_to :publisher# etc.endwithout having to worry about how ActiveRecord::Base ended up having amethod called belongs_to.

Looks weird? Yes to me. Luckily, I found a thorough explanation in "Ruby for Rails"

$:.unshift(File.dirname(__FILE__))

This line adds the directory containing active_record.rb to the loadpath ofrequire. It does this in the following way:

■ The variable $: holds the loadpath, which determines the search orderused by require.■ __FILE__ is a special Ruby variable that holds the name of the current file:active_record.rb.■ File.dirname returns the directory part of the full path- and filename of thefile—in this case, /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.9.1/lib or equivalent.■ The unshift operation adds that directory to the front of the load path.

NOTE THE POSITION OF THE CURRENT DIRECTORY IN THE RUBY LOADPATHBy default, the Ruby loadpath includes the current directory, indicatedby the single dot (.) at the end of the list of load directories. (You can seethem all if you do ruby -e ‘p $:’.) Also by default, the current directoryis whatever directory was current when the program started executing; soif you’re in directory zero and give the command ruby one/two/prog.rb, prog.rb will consider zero (not two) to be its runtime currentdirectory. This means that even if two files are in the same directory, youcan’t necessarily just require one from the other without either modifyingthe loadpath ($:) or using a full pathname in the require statement.The upshot of all this is that Rails does a fair amount of directory andloadpath manipulation, so that the files that need to see each other canindeed see each other.

As we all know, there are different ways to do the same thing on Windows. For example, to save a document, we could either click File->save from menu or punch Ctrl-S on keyboard, I call them alternatives. I, as a QA, need to test alternatives. Ruby is once again show its powerful dynamic weapon, and its quite easy to accomplish this with the below code.

1:classObject

2: def self.inherited(subclass)

3:if @subclasses

4: @subclasses << subclass

5:else

6: @subclasses = [subclass]

7:end

8:end

9:

10: def self.subclasses

11: @subclasses

12:end

13:end

14:

15:class Array

16: def random

17: self[rand(self.length)]

18:end

19:end

20:

21:

22:class Talkable

23: def talk

24: raise "This is abstract method that must be implemented in the derived class"

25:end

26: def self.do_some_talk

27: talker = self.subclasses.random.new

28: talker.talk

29:end

30:end

31:

32:class Chinese < Talkable

33: def talk

34: puts "Ni hao"

35:end

36:end

37:

38:class American < Talkable

39: def talk

40: puts "Hi"

41:end

42:end

43:

44:class Geek < Talkable

45: def talk

46: puts "..."

47:end

48:end

49:

50: Talkable.do_some_talk

There are some interesting things happened in the above lines of code 🙂

1. Line#1-#13, As there is no build-in method for looking for all subclass. We need to invent it.

2. Line#15-#19, Similarly, define a random method to randomly pick up an item from Array.

3. Line#23-#25, An interface in ruby, don’t know it other do it this way, but it works 🙂

It confused me for days, and it’s more frustrated to find those answers with dummy descriptions. And finally, when I am almost plan to quit, I find this. Although in Japanese, it’s clear enough for me to understand these words:)

1: gem uninstall rubyforge --version=1.0.0

2: gem install rubyforge --version=0.4.5

Actually, I only need to run first command to run make rake db:migrate task. Anyone know Japanese, translate this for me 🙂