1

What does it mean when the bang method is in front? What is this shorthand for?

!post.save
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
alenm
  • 1,013
  • 3
  • 15
  • 34

2 Answers2

6

It's a negation. In your example it means to NOT the result of post.save.

if:

post.save => true
!post.save => false

otherwise:

post.save => false
!post.save => true
TreyE
  • 2,649
  • 22
  • 24
6

It is equivalent to

not post.save

Usually used in if clauses, like:

if !post.save               #if the post could not be saved for some reason
   puts 'could not save post!'
end

It's because the function save from ActiveResource::Base returns true if the POST request succeeded and false if it didn't. Read here for some more information about the function.

Ivan Zarea
  • 2,174
  • 15
  • 13