Created
January 30, 2011 17:35
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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