【发布时间】:2017-09-28 14:53:28
【问题描述】:
我最近学习了 ruby 和 rails,并在 ruby 中看到了 :foo 语法和 attr_accessor 之类的东西,在带有模型方法的 rails 中,但我不太明白。这种语法的名称是什么,它的作用是什么?
已见案例示例
class Dog
attr_accessor :name
end
///second scenario
class CreatePurchases < ActiveRecord::Migration
def change
create_table :purchases do |t|
t.string :name
t.float :cost
t.timestamps
end
end
end
//third scenario
class Purchase < ActiveRecord::Base
validates :name, presence: true
validates :cost, numericallity: {greater than : 0}
end
【问题讨论】:
-
我真的不知道 attr_accessor 做了什么,我在问
:foo语法 -
它们在Ruby中被称为符号,可以帮助您在线搜索文章。
标签: ruby-on-rails ruby