class Books < ActiveRecord::Migration
def self.up
create_table :books do |t|
t.column :title, :string, :limit => 32, :null => false
t.column :price, :float
t.column :subject_id, :integer
t.column :description, :text
t.column :created_at, :timestamp
end
end
def self.down
drop_table :books
end
end
create_table :books do |t|
这一句 没有迭代操作 为什么会有 ruby do |t|
这句呢 这里的 ruby do |t|
到底是什么意思? 求高人解答
Not an iteration but a callback.
About callbacks in Ruby:
http://stackoverflow.com/questions/1677861/how-to-implement-a-callback-in-ruby
About Rails Migration:
http://guides.rubyonrails.org/migrations.html
If you have done Ajax with jQuery, you should have an experience similar to this:
The return value of the$.get()
method is the response body of test.php. The first parameter is the requested URL, and the second parameter is the callback function. This function accepts the response body of test.php as the value of the parameter data, and passes the pop-up window show.You can understand this Migration statement this way.
Here: /q/1010000000266437 I’ve already gone back and moved it here again.
do|x|...end has no special meaning, just like {|x|}, it just represents a block. There is iteration in this code, it is actually similar to:
Yes, of course this is also legal:
Then it becomes like the above because the brackets can be omitted.
For ruby to implement such a function, you only need:
For more specific usage of iterators, you can read this: http://blog.csdn.net/classwang/article/details/4692856
do ... end is equivalent to { ... }, which is a block. Ruby methods can accept block parameters