Module: TheForce::ObjectSupport::InstanceMethods
- Included in:
- Object
- Defined in:
- lib/the_force/object_support.rb
Instance Method Summary collapse
-
#eigenclass ⇒ Object
this could also be labeled ‘self’?.
-
#if(*args, &b) ⇒ Object
Will return a nil if the condition is false, otherwise it will return the receiver.
-
#unless(*args, &b) ⇒ Object
Same as
if, only with the predictably inverse logic.
Instance Method Details
#eigenclass ⇒ Object
this could also be labeled ‘self’?
78 79 80 |
# File 'lib/the_force/object_support.rb', line 78 def eigenclass #this could also be labeled 'self'? class << self; self; end end |
#if(*args, &b) ⇒ Object
Will return a nil if the condition is false, otherwise it will return the receiver. The condition can be specified in 3 ways, as a list of arguments which are sent to Object#send, a block, or via essentially a delegate object, ala the andand gem.
Examples
-
10.if(:>, 5) => 10
-
10.if {|x| x == 5) => nil
-
10.if.nonzero? => 10
Usage
This becomes useful to simplify logic when you want to specify a default value, (particularly in Rails views) or you have a receiver which is the result of a lengthy expression.
-
number_of_products.if.nonzero? || “Don’t you want to buy something?”
-
lovers.sort.reverse.unless.empty? || “Where’s the fever? :(”
Warning
Do not try to do:
-
title.if.length > 20 || title.truncate + “…”
This would evaluate to:
-
title > 20 || title.truncate(17) + “…”
66 67 68 |
# File 'lib/the_force/object_support.rb', line 66 def if(*args, &b) TheForce::ObjectSupport.conditional(self, false, *args, &b) end |
#unless(*args, &b) ⇒ Object
Same as if, only with the predictably inverse logic.
Usage
-
title.unless.blank? || suggestion_html “please set your title on the edit page”
74 75 76 |
# File 'lib/the_force/object_support.rb', line 74 def unless(*args, &b) TheForce::ObjectSupport.conditional(self, true, *args, &b) end |