The author’s review on this design is also great:
If you find this convoluted, don’t feel discouraged. It is—but it’s convoluted
for the sake of clean organization. Instead of writing belongs_to and the other
association methods directly as class methods of ActiveRecord::Base, Rails puts
them in a module that clearly labels them with the role they’re going to play:
ActiveRecord::Associations::ClassMethods. Then, ActiveRecord::Base is extended
with that module, at which point things proceed as if that module’s
instance methods were class methods of ActiveRecord::Base the whole time. The
best of both worlds is preserved: The code remains organized and labeled with
meaningful class and module names, while the programmer can do things like:
class Edition < ActiveRecord::Base
without having to worry about how ActiveRecord::Base ended up having a
method called belongs_to.
Looks weird? Yes to me. Luckily, I found a thorough explanation in "Ruby for Rails"
This line adds the directory containing active_record.rb to the loadpath of
require. It does this in the following way:
■ The variable $: holds the loadpath, which determines the search order
used by require.
■ __FILE__ is a special Ruby variable that holds the name of the current file:
■ File.dirname returns the directory part of the full path- and filename of the
file—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 LOADPATH
By default, the Ruby loadpath includes the current directory, indicated
by the single dot (.) at the end of the list of load directories. (You can see
them all if you do ruby -e ‘p $:’.) Also by default, the current directory
is whatever directory was current when the program started executing; so
if 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 current
directory. This means that even if two files are in the same directory, you
can’t necessarily just require one from the other without either modifying
the loadpath ($:) or using a full pathname in the require statement.
The upshot of all this is that Rails does a fair amount of directory and
loadpath manipulation, so that the files that need to see each other can
indeed see each other.
This is the collection of various problems I met during my learning of Ruby. Hope it could also help you 🙂
|problem of installing json gem.||ERROR: Error installing json: |
ERROR:Failed to build gem native extension.
|gem install json_pure|
|E-textEditor||Can’t run ruby problem||remove RUBYOPT environment variable.|
|Install local gems||gem install –local filename.gem|
|Add gem source||gem sources -a <gem source>|
|Remove gem source||gem sources -r <gem source>|
|Install gems using specified server||gem install <gemName> -s <serverName>|
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.
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:)
Actually, I only need to run first command to run make rake db:migrate task. Anyone know Japanese, translate this for me 🙂
redmineを動かそうと script/server したらタイトルのようなエラーが。Googleで探せどもいまいちしっくりくる解決策がありません。
そもそも、rubyforge-1.0.0を必要としてるのは何か、と調べてみたら、hoeの1.5.3でした。hoeの1.5.1までは rubyforge 0.4.5を使っていて、hoeの1.5.3は1.0.0を使っているとのこと。で、ZenTestのようにhoeに依存しているものは、1.5.1以上であればいい模様。ということで、
gem install rubyforge –version=0.4.5
gem uninstall hoe –version=1.5.3
gem uninstall rubyforge –version=1.0.0
Windows + e texteditor + cgywin + console2
a) 安装e texteditor
初次运行e texteditor就会提示安装cgywin,直接选automatic install就好了，e texteditor会自动下载cgywin
选add tab, 在shell里面输入:
c:cygwinbinbash –login –i
Download RubyGem code
tar –xzvf rubygems-1.1.0.tgz
Sudo ruby setup.rb
sudo gem update –system
sudo gem install rails –include-dependencies
你将会看到e texteditor 自动启动并将当前目录的项目打开。