Created
April 14, 2016 11:41
-
-
Save ProGM/f2266be32d7e74b8265e08848a8a3daf to your computer and use it in GitHub Desktop.
An implementation for `dig` method for generic objects.
This file contains hidden or 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
class Object | |
def dig(*methods) | |
output = self | |
methods.each { |method| output = output&.public_send(method) } | |
output | |
end | |
end | |
class MyClass | |
attr_accessor :some_attribute | |
end | |
# Usage example: | |
object = MyClass.new | |
object.dig(:some_attribute, :method1, :method2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment