Project

General

Profile

Actions

Feature #17155

open

Add a Diggable mixin

Added by janfri (Jan Friedrich) about 4 years ago. Updated about 4 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:99943]

Description

Ruby 2.3 introduced the #dig method as a general way to access elements in deep structures. Initially it was implemented only for Array and Hash. In the meanwhile also the classes Struct, OpenStruct and Set have a #dig method. There is also a proposal to implement #dig for Thread.

In my development I have own classes which have a #[] method implemented and are also candidates for a #dig method. But implementing #dig isn't so trivial as it seems: you have to handle some corner cases. This is the intention for my proposal: Add a Diggable mixin to Ruby (core or stdlib). A simple implementation of it could be something like this:

module Diggable

 def dig *args
   a = args.shift
   o = self[a]
   return o if args.empty? || o.nil?
   o.send :dig, *args
 end

end

So you could simply include Diggable to any class which implements #[], and you would get a #dig method for free. Like Comparable or Enumerable for which you have to implement #<=> respectively #each and get other methods implemented in this mixins.

Actions #1

Updated by janfri (Jan Friedrich) about 4 years ago

  • Description updated (diff)
Actions

Also available in: Atom PDF

Like0
Like1