开发者

what does an exclamation mark follow with variable mean in ruby on rails? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Why are exclamation marks used in Ruby methods?

i am reading tutorials for Rails3 with MongoDB

http://www.mongodb.org/display/DOCS/MongoDB+Data+Modeling+and+Rails

and i see this 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,   开发者_Go百科  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


in general, when a 'bang' follows a method in Ruby it will change the source.

for example check out the following output:

irb(main):007:0> x = 'string'
=> "string"
irb(main):008:0> x
=> "string"
irb(main):009:0> x.capitalize
=> "String"
irb(main):010:0> x
=> "string"
irb(main):011:0> x.capitalize!
=> "String"
irb(main):012:0> x
=> "String"

x.capitalize returns "String", but variable x remains lower case. When I add the ! ('bang') to the end var x is modified.

i am not overall familiar with mongodb, but this may shed some light onto the purpose of the bang in ruby.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜