Class: OpenStruct
- Inherits:
-
Object
- Object
- OpenStruct
- Defined in:
- lib/ostruct.rb
Overview
patching OpenStruct due to Ruby 2.3.1 upgrade See more here: stackoverflow.com/questions/39278864/openstruct-issue-with-ruby-2-3-1/39280374#39280374
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(hash = nil) ⇒ OpenStruct
constructor
A new instance of OpenStruct.
-
#method_missing(mid, *args) ⇒ Object
:nodoc:.
Constructor Details
#initialize(hash = nil) ⇒ OpenStruct
Returns a new instance of OpenStruct.
5 6 7 8 9 10 11 12 |
# File 'lib/ostruct.rb', line 5 def initialize(hash = nil) @table = {} hash&.each_pair do |k, v| k = k.to_sym @table[k] = v new_ostruct_member(k) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mid, *args) ⇒ Object
:nodoc:
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ostruct.rb', line 34 def method_missing(mid, *args) # :nodoc: mname = mid.id2name len = args.length if mname.chomp!('=') if len != 1 raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1) end modifiable[new_ostruct_member(mname)] = args[0] elsif len == 0 @table[mid] else err = NoMethodError.new "undefined method `#{mid}' for #{self}", mid, args err.set_backtrace caller(1) raise err end end |