Skip to content

Instantly share code, notes, and snippets.

@ProGM
Created April 14, 2016 11:41
Show Gist options
  • Save ProGM/f2266be32d7e74b8265e08848a8a3daf to your computer and use it in GitHub Desktop.
Save ProGM/f2266be32d7e74b8265e08848a8a3daf to your computer and use it in GitHub Desktop.
An implementation for `dig` method for generic objects.
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