ruby on Rails 中变量后面的感叹号是什么意思?

发布于 2024-11-14 08:13:42 字数 812 浏览 1 评论 0原文

我正在阅读 Rails 3 with mongodb 的教程

,我看到类似这样的内容,

  # Note this: ids are of class ObjectId.
  key :user_id,   ObjectId
  timestamps!

感叹号是什么意思??? 谢谢。

  class Story
      include MongoMapper::Document

      key :title,     String
      key :url,       String
      key :slug,      String
      key :voters,    Array
      key :votes,     Integer, :default => 0
      key :relevance, Integer, :default => 0

      # Cached values.
      key :comment_count, Integer, :default => 0
      key :username,      String

      # Note this: ids are of class ObjectId.
      key :user_id,   ObjectId
      timestamps!

      # Relationships.
      belongs_to :user

      # Validations.
      validates_presence_of :title, :url, :user_id
    end

i was reading tutorials for rails 3 with mongodb

and i see something like this

  # Note this: ids are of class ObjectId.
  key :user_id,   ObjectId
  timestamps!

what does the exclamation mark mean???
Thanks.

  class Story
      include MongoMapper::Document

      key :title,     String
      key :url,       String
      key :slug,      String
      key :voters,    Array
      key :votes,     Integer, :default => 0
      key :relevance, Integer, :default => 0

      # Cached values.
      key :comment_count, Integer, :default => 0
      key :username,      String

      # Note this: ids are of class ObjectId.
      key :user_id,   ObjectId
      timestamps!

      # Relationships.
      belongs_to :user

      # Validations.
      validates_presence_of :title, :url, :user_id
    end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无名指的心愿 2024-11-21 08:13:42

这是在 MongoMapper::Document 中定义的:

https://github .com/jnunemaker/mongomapper/blob/master/lib/mongo_mapper/plugins/timestamps.rb

    def timestamps!
      key :created_at, Time
      key :updated_at, Time
      class_eval { before_save :update_timestamps }
    end

That's defined in MongoMapper::Document:

https://github.com/jnunemaker/mongomapper/blob/master/lib/mongo_mapper/plugins/timestamps.rb

    def timestamps!
      key :created_at, Time
      key :updated_at, Time
      class_eval { before_save :update_timestamps }
    end
巴黎盛开的樱花 2024-11-21 08:13:42

在 Ruby 中,方法可以以问号(method_name?)或感叹号(method_name!)结尾。

它们的语义取决于程序员。有一个约定,使用感叹号来指示该方法将修改它所调用的对象,但很多人将它们用于其他目的。

在你的例子中,我想,它的意思是“做吧!”,一目了然地表明该方法将产生一些“有趣的”副作用。

In Ruby, methods are allowed to end with question marks (method_name?) or exclamation marks (method_name!).

Semantics of them is up to programmer. There is a convention to use exclamation marks to indicate that method will modify the object it's called on, but plenty of folk use them for other purposes.

In your case, I suppose, it means something like "do it!", to make obvious at a glance that the method is going to have some "interesting" side effect.

浮光之海 2024-11-21 08:13:42

哇!这完全证明了为什么我对 Rails 是一个完全荒谬的框架的怀疑都得到了证实!让我们让开发人员决定某些内容的含义。真的吗?!

http://phpvsrails.blogspot.com/

这说明了一切。

Wow! This just completely officiated why my suspicions about Rails being a totally ridiculous framework are all substantiated! Let's let developers dictate what something means. Really?!

http://phpvsrails.blogspot.com/

That about says it all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文