module ArgParser::Tools

Aux tools intented to include into a class

Public Instance Methods

hash2vars(hash) click to toggle source

Sets self state from a hash given

# File lib/argparser/tools.rb, line 7
def hash2vars(hash)
  if hash.kind_of?(Hash) || (hash.respond_to?(:to_h) && (hash = hash.to_h))
    hash.each do |k, v|
      next unless self.respond_to?(k)
      instance_variable_set("@#{k}", v)
    end
  else
    raise 'Hash expected'
  end
  self
end
to_hash() click to toggle source

Returns a hash of self state

# File lib/argparser/tools.rb, line 20
def to_hash
  instance_variables.reduce({}) { |hash, var|
    hash[var[1..-1]] = instance_variable_get(var)
    hash }
end