Class: HStruct

Inherits:
Struct
  • Object
show all
Defined in:
lib/hstruct.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ HStruct

Returns a new instance of HStruct.

Raises:

  • (ArgumentError)


2
3
4
5
6
7
8
9
10
11
12
# File 'lib/hstruct.rb', line 2

def initialize(args = {})
  raise ArgumentError unless args.is_a?(Enumerable)

  unless args.keys.first.is_a?(Symbol)
    args = args.each_with_object({}) do |(k,v), result|
      result[k.to_sym] = v
    end
  end

  super(*members.map { |m| args[m] })
end

Instance Method Details

#to_hObject



14
15
16
# File 'lib/hstruct.rb', line 14

def to_h
  Hash[each_pair.to_a]
end