Module: Args
- Included in:
- Arp, Ethernet::Frame
- Defined in:
- lib/ruby-ext.rb
Instance Method Summary collapse
Instance Method Details
#ivars ⇒ Object
108 109 110 |
# File 'lib/ruby-ext.rb', line 108 def ivars instance_variables.reject { |x| x =~ /^@_/ }.collect { |x| x[1..-1].to_sym } end |
#set(h) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ruby-ext.rb', line 63 def set(h) for key in [ivars].flatten if h.has_key?(key) and ! h[key].nil? begin klassname = key.to_klass if self.class.const_defined?(klassname) instance_variable_set("@#{key.to_s}", self.class.const_get(klassname).new(h[key])) elsif self.respond_to?(key.to_setter) self.send key.to_setter, h[key] else instance_variable_set("@#{key.to_s}", h[key]) end rescue ArgumentError => e raise ensure #h.delete(key) end else end end end |
#to_hash ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ruby-ext.rb', line 85 def to_hash h = {} for key in [ivars].flatten ivar = instance_variable_get("@#{key.to_s}") if ivar.respond_to?(:to_hash) h.store(key,ivar.to_hash) elsif ivar.is_a?(Array) h.store(key, ivar.collect { |x| if x.respond_to?(:to_hash) x.to_hash else # x.to_s nil end } ) else h.store(key,ivar) unless ivar.nil? end end h end |