ruby-on-rails - ruby on rails中出现symbol使用的问题 急求大神解答
高洛峰
高洛峰 2017-04-21 11:16:35
0
1
535
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中的符号是用来替代字符串节省内存空间 可是这个情形下依然不知道是什么意思啊 求大神解答

<%= f.label :name, class: "right inline" %>

<%= f.text_field :name %>

<%= f.label :price, class: "right inline", title: "Price in USD", data: {tooltip: true} %>

<%= f.text_field :price %>

<%= f.submit %>

还有在erb中也出现了ruby符号的奇怪用法 f.label :price 是要用:price 去代替f.label吗? 急求大神解答 纠结了几天了

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all (1)
大家讲道理

:xxxinruby represents a symbol.

You can understand symbolinto string, but they are not exactly the same.

symbol is immutable.

First of all, everything in ruby is anobject. 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

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!