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
在上面rails迁移代码中:books是用符号代表参数吗? :title :price :string这些都是要干什么捏? 我知道ruby中的符号是用来替代字符串节省内存空间 可是这个情形下依然不知道是什么意思啊 求大神解答
<p class="row collapse">
<p class="small-3 columns">
<%= f.label :name, class: "right inline" %>
</p>
<p class="small-9 columns"><%= f.text_field :name %></p>
</p>
<p class="row collapse">
<p class="small-3 columns">
<%= f.label :price, class: "right inline", title: "Price in USD", data: {tooltip: true} %>
</p>
<p class="small-9 columns"><%= f.text_field :price %></p>
</p>
<p class="row collapse">
<p class="small-9 small-offset-3 columns"><%= f.submit %></p>
</p>
还有在erb中也出现了ruby符号的奇怪用法 f.label :price 是要用:price 去代替f.label吗? 急求大神解答 纠结了几天了
:xxx in ruby represents a symbol.
You can understand symbol into string, but they are not exactly the same.
symbol is immutable.
First of all, everything in ruby is an object. Each object has a unique object_id that represents its physical location in memory (but it is not directly accessed by this object).
However, there are some exceptions, such as integers. You will find that the object_id of the same integer is the same. This is Ruby's way of saving resources.
This is the implementation of ruby. Symbols are the same as constants. The same symbols use the same object_id, that is, their locations in memory are the same. (The object_id of a constant directly reflects the value of the constant, and is also treated specially during processing).
As for the code above. The parentheses for function calls in Ruby can be omitted. For example, f.label :price is actually f.label( :price) At the same time, the {} of hash can also be omitted. This is what you see: xxx=>xxx,xxx=>xxx or xxx:xxx,xxx:xxx. They are actually {:xxx=>xxx, ...}
:price is just one of its parameters.
So it appeared
Code like this, What he means is,
In this way, he will create a label under form f, the name is: name, and the class in the html tag is "right inline".
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