Skip to content

Instantly share code, notes, and snippets.

@unicornrainbow
Created January 30, 2011 17:35
Show Gist options
  • Save unicornrainbow/803040 to your computer and use it in GitHub Desktop.
Save unicornrainbow/803040 to your computer and use it in GitHub Desktop.
Get at properties of values that might not be present with out checking for nil.
# Usage:
# nil.get :blake # => nil
# some_obj.get :name, :length # => The value of some_obj.name.length or nil
class Object
def get(*properties)
object = self
properties.each do |property|
if object.respond_to?(property)
object = object.send(property)
else
return nil
end
end
object
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment