Ruby on rails has a problem with symbol, and it is urgent to find a great solution.

  question, ruby
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

In the rails migration code above: Are books symbols used to represent parameters? What are these things for? I know that symbols in ruby are used to replace strings to save memory space, but I still don’t know what it means in this case.

<div class="row collapse">
 <div class="small-3 columns">
 <%= f.label :name, class: "right inline" %>
 </div>
 <div class="small-9 columns"><%= f.text_field :name %></div>
 </div>
 <div class="row collapse">
 <div class="small-3 columns">
 <%= f.label :price, class: "right inline", title: "Price in USD", data: {tooltip: true} %>
 </div>
 <div class="small-9 columns"><%= f.text_field :price %></div>
 </div>
 <div class="row collapse">
 <div class="small-9 small-offset-3 columns"><%= f.submit %></div>
 </div>

There is also a strange use of ruby symbol in erb. f.label :price is to use: price instead of f.label? It has been a few days since I urgently asked the great god to solve the problem.

In ruby:xxxRepresents a symbol.

You can put symbolUnderstandingString, but they are not exactly the same.

Symbol is unchanged.

First, everything in ruby isObjectEach object has a unique object_id that represents its physical location in memory (but it is either directly accessed or the 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 to save resources.

VALUE
 rb_obj_id(VALUE obj)
 {
 
 if (TYPE(obj) == T_SYMBOL) {
 return (SYM2ID(obj) * sizeof(RVALUE) + (4 << 2)) | FIXNUM_FLAG;
 }
 if (SPECIAL_CONST_P(obj)) {
 return LONG2NUM((long)obj);
 }
 return (VALUE)((long)obj|FIXNUM_FLAG);
 }

This is ruby’s implementation. symbol is the same as a constant. The same symbol uses the same object_id, which means that their locations in memory are the same. (The object_id of a constant directly reflects the value of the constant, and is also specially processed during processing).

As for the above code. The brackets for function calls in ruby can be omitted, for example, f.label :price is actually f.label( :price)
At the same time, hash {} can also be omitted, which is what you see: xxx=>xxx,xxx=>xxx or xxx:xxx,xxx:xxx, they are actually {:xxx=>xxx,…}

Price is just one of his parameters.

So there it is

f.label :name, class: "right inline"

Such code,
What he meant was,

f.label (:name, {:class => "right inline"})

In this way, he will create a label under form f, name is: name, and the class in the html label is “right inline.”

create_table :books do |t|

Do|x|…end has no special meaning, just like {|x|}, it just represents a block.
There are iterations in this code, which is actually similar to:

File.open("xxx","xxx") do |f|
 f.write(...)
 end

Of course, this is also legal:

File.open("xxx","xxx") { |f|
 f.write(...)
 }

Then, because the brackets can be omitted, it will look like the above.

For ruby to implement such a function, all it needs is:

class Somethings
 #  ...
 def create_table(name)
 # Make some preparations for creating this table ...
 #   ...
 yield @table
 # Create this table
 #   ...
 end
 end

For more specific usage of iterators, take a look at this:http://blog.csdn.net/classwang/article/details/4692856