Ruby的NilClass實例中的to_i方法返回零的事實背後的科學是什麼?返回無或引發異常不會更符合邏輯?Ruby Nil and Zero
回答
NilClass
定義#to_i
出於同樣的原因它定義了一個#to_a
返回[].
它給你正確的類型,但空之類的價值的東西。
這實際上非常有用。例如:
<%= big.long.expr.nil? ? "" : big.long.expr %>
變爲:
<%= big.long.expr %>
的效果好很多! (ERB是調用#to_s其中對於零,爲 「」)。而且:
if how.now.brown.cow && how.now.brown.cow[0]
how.now.brown.cow[0]
else
0
end
變爲:僅需要表示時
how.now.brown.cow.to_a[0].to_i
短轉化存在。長時間轉換是Ruby核心方法調用的轉換,它們需要非常接近的東西。如果您想要進行類型檢查,請使用它們。
即:
thing.to_int # only works when almost Integer already. NilClass throws NoMethodError
thing.to_i # this works for anything that cares to define a conversion
*「好多了!」*是的,這取決於你和項目工作的其他人都知道Ruby以及你想要依賴不直觀行爲的程度。返回0而不是引發異常的'nil.to_i'在邏輯上沒有太大的意義。你怎麼能從'nil'得到'0'?它不像'nil == 0'返回true,但'to_i'意味着等價。 –
@Ed S .:不,'to_int'意味着等價,'to_i'非常鬆散,它並不意味着什麼。 –
偉大的答案!我總是忽略了長期的轉換,現在我可以理解它們以及紅寶石哲學中的合適性。 –
它適合放縱的紅寶石理念(相對於,例如,Python的嚴格性):
nil.to_i #=> 0
"".to_i #=> 0
"123hello".to_i #=> 123
"hello".to_i #=> 0
說實話,我認爲這是過於寬鬆。正如Zabba所述,您可以使用Kernel#Integer(string)進行嚴格轉換。
to_i
表示「如果可以的話,將我轉換爲整數」。
如果你想要「如果你非常像整數,給我你的整數值,否則給一個NoMethodError」,然後使用.to_int
。
還有另外一個問題,詢問有關to_i
和to_int
之間的區別,to_s
與to_str
等讓我知道,如果你想我幫您找到它。
的to_i
協議說,你必須返回的Integer
,你不能引發異常。你的兩條建議至少違反了其中一條規則。所以,不,這些不僅不會更合乎邏輯,而且會變得無效。
但是,請注意,nil
不會迴應to_int
。如果它確實對to_int
作出迴應,那確實是「不合邏輯的」。
協議說'to_i'必須返回一個整數並不會使這種行爲變得合乎邏輯,它只是使其符合規定(假設,即使不合邏輯,也最好符合它)。 –
如果你碰巧在Rails的,那麼你可以使用try
:
nil.to_i # => 0
nil.try :to_i # => nil
- 1. false && nil and nil && false in ruby
- 2. C++ Functors and Zero
- 3. NumericTraits Zero and One
- 4. Chef Vault with Test-Kitchen,Vagrant and Chef-Zero provisioner
- 5. Ruby和Nil對象
- 6. webrick and ruby interpreter
- 7. Ruby EventMachine and Multicast
- 8. Ruby and PHP togheter
- 9. ruby and ruby on rails book(2009)
- 10. Ruby on Rails to_xml nil =「True」
- 11. Ruby on Rails 5 and Devise:Overriding after_sign_out_path_for
- 12. Ruby - Threads and Dir [] arrays
- 13. Ruby on Rails:link_to_remote and rel
- 14. ruby on rails and mongo
- 15. true-false或zero-and-ones數組的熱圖
- 16. ruby數組迭代和檢查nil值
- 17. Ruby字符串「nil」爲零:NilClass nil(反向的nil.inspect)
- 18. ruby method_missing class and module中的用法
- 19. ruby case case statement and compare
- 20. ruby「each do」and scriptaculous Effect.BlindDown/Up
- 21. ruby on rails,searchlogic and refactoring
- 22. Heroku with ruby on rails and Mongoid
- 23. ruby Class.new在rspec中返回nil
- 24. 爲什麼Ruby中的a = a`` nil`?
- 25. Ruby on Rails ENV ['RAILS_ENV']返回nil
- 26. Ruby放在救援中顯示nil
- 27. 混淆`nil`輸出時,在Ruby中
- 28. 在Ruby中,爲什麼!nil return => true?
- 29. image naturalWidth return zero
- 30. Convert.ToInt32 - Keep Preceding Zero
相關:您可以使用'N =整數(STR)'如果你想提高對故障異常。 – Dogbert