Skip to content
This repository
Browse code

AS guide: documents Object#presence

  • Loading branch information...
commit 7f8d2cdb1715ddfaa27eed892df7ca9884e5315a 1 parent 2e59151
Xavier Noria authored December 30, 2009
8  railties/guides/source/active_support_core_extensions.textile
Source Rendered
@@ -48,6 +48,14 @@ The method +present?+ is equivalent to +!blank?+:
48 48
 assert @response.body.present? # same as !@response.body.blank?
49 49
 </ruby>
50 50
 
  51
+h4. +presence+
  52
+
  53
+The +presence+ method returns its receiver if +present?+, and +nil+ otherwise. It is useful for idioms like this:
  54
+
  55
+<ruby>
  56
+host = config[:host].presence || 'localhost'
  57
+</ruby>
  58
+
51 59
 h4. +duplicable?+
52 60
 
53 61
 A few fundamental objects in Ruby are singletons. For example, in the whole live of a program the integer 1 refers always to the same instance:

0 notes on commit 7f8d2cd

Please sign in to comment.
Something went wrong with that request. Please try again.