2012-01-24 34 views
1

爆炸方法在前面是什麼意思?這是什麼速記?是什麼!意味着在一個方法之前?

!post.save 
+0

是否與[this]相同(http://stackoverflow.com/questions/612189/why-are-exclamation-marks-used-in-ruby-methods)?我不知道Ruby。 – Ryan

+1

@minitech,不,它不是。 –

+0

@ KL-7:你是對的,不是。我無法相信我沒有注意到這一點。 – Ryan

回答

6

它相當於

not post.save 

一般在if子句中使用,如:

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

這是因爲如果POST請求成功,則從ActiveResource::Base保存的函數返回true,如果不成功,則返回falseRead here有關該功能的更多信息。

6

這是否定。在你的例子中,它意味着不是post.save的結果。

如果:

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

否則:

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