Ruby:它在类中声明了什么?

时间:2013-12-04 01:43:16

标签: ruby

我从datamapper http://datamapper.org/getting-started.html

获得了此示例代码
class Post
  include DataMapper::Resource

  property :id,         Serial    # An auto-increment integer key
  property :title,      String    # A varchar type string, for short strings
  property :body,       Text      # A text block, for longer string data.
  property :created_at, DateTime  # A DateTime, for any date you might like.
end

有谁能告诉我“财产”是如何产生的?它是函数,变量,类变量或实例变量还是常量?

有时候我也看到了这种代码

class CarModel
   attribute :name
   attribute :hello
end

但不知道如何生成

1 个答案:

答案 0 :(得分:2)

这是您执行以下操作时包含的方法:

include DataMapper::Resource

如果您有兴趣深入挖掘,可以看到其源代码here

它基本上将属性添加到Post资源中的属性列表中。