Home  >  Q&A  >  body text

ruby-on-rails - ruby on rails中出现symbol使用的问题 急求大神解答

  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吗? 急求大神解答 纠结了几天了

高洛峰高洛峰2712 days ago576

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-04-21 11:18:35

    :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.

    
    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 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

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

    Code like this, What he means is,

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

    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".

    create_table :books do |t|
    

    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:

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

    Yes, of course this is also legal:

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

    Then it becomes like the above because the brackets can be omitted.

    For ruby ​​to implement such a function, you only need:

    class Somethings
    #...  
      def create_table(name)
        # 为创建这个表做一些准备……
        # ...
        yield @table
        # 创建这个表
        # ...
      end
    end
    

    For more specific usage of iterators, you can read this: http://blog.csdn.net/classwang/article/details/4692856

    reply
    0
  • Cancelreply