0 Replies - 1691 Views - Last Post: 16 November 2013 - 04:44 PM

#1 Lemur   User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1453
  • View blog
  • Posts: 3,633
  • Joined: 28-November 09

If it exists

Posted 16 November 2013 - 04:44 PM

Description: s = 'I exist' message = s.if_it_exists # => 'I exist' s = nil message = s.if_it_exists # => nil 1.if_it_exists # => 1Many times you find yourself needing to set something equal to something that may or may not exist. This is a nifty way to skiff that annoyance.
class Object
  def if_it_exists
    self ? self : nil
  end
end



Is This A Good Question/Topic? 0
  • +

Page 1 of 1