Class: Struct

Inherits:
Object show all
Defined in:
lib/backports/2.3.0/struct/dig.rb,
lib/backports/2.0.0/struct/to_h.rb

Instance Method Summary collapse

Instance Method Details

#dig(key, *rest) ⇒ Object

Raises:

  • (TypeError)

3
4
5
6
7
8
9
# File 'lib/backports/2.3.0/struct/dig.rb', line 3

def dig(key, *rest)
  return nil unless respond_to?(key)
  val = public_send(key)
  return val if rest.empty? || val == nil
  raise TypeError, "#{val.class} does not have #dig method" unless val.respond_to? :dig
  val.dig(*rest)
end

#to_hObject


3
4
5
6
7
# File 'lib/backports/2.0.0/struct/to_h.rb', line 3

def to_h
  h = {}
  self.class.members.each{|m| h[m.to_sym] = self[m]}
  h
end