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.



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

def initialize(args = {})
  ensure_enumerable(args)

  args = args.each_with_object({}) do |(k,v), result|
    result[k.to_sym] = v
  end

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

Instance Method Details

#ensure_enumerable(args) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
# File 'lib/hstruct.rb', line 21

def ensure_enumerable(args)
  raise ArgumentError unless args.is_a?(Enumerable)
end

#to_hObject



25
26
27
# File 'lib/hstruct.rb', line 25

def to_h
  Hash[each_pair.to_a]
end

#update(args = {}) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/hstruct.rb', line 12

def update(args={})
  ensure_enumerable(args)

  args.each do |(k,v)|
    self[k.to_sym] = v
  end
  self
end