ruby on Rails 中变量后面的感叹号是什么意思?
我正在阅读 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是在 MongoMapper::Document 中定义的:
https://github .com/jnunemaker/mongomapper/blob/master/lib/mongo_mapper/plugins/timestamps.rb
That's defined in MongoMapper::Document:
https://github.com/jnunemaker/mongomapper/blob/master/lib/mongo_mapper/plugins/timestamps.rb
在 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.
哇!这完全证明了为什么我对 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.