2

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,     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
Community
  • 1
  • 1
jojo
  • 12,563
  • 30
  • 82
  • 119

1 Answers1

0

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.

matchew
  • 17,544
  • 4
  • 40
  • 44
  • 1
    Just a note here, the bang used in destructive methods (that is, that change the source), is merely a convention - and Ruby is full of conventions. – Chubas Jun 07 '11 at 15:40
  • thanks, I am a complete new comer to ruby, but questions like this I like to understand. So I spent some time on it last night, knowing, this topic would likely be closed. Thanks for clearing that up! +1 – matchew Jun 07 '11 at 15:46