Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/logging/utils.rb

Overview


Instance Method Summary collapse

Instance Method Details

#getopt(*args) ⇒ Object

call-seq:

getopt( key, default = nil, :as => class )

Returns the value associated with the key. If the has does not contain the key, then the default value is returned.

Optionally, the value can be converted into to an instance of the given class. The supported classes are:

Integer
Float
Array
String
Symbol

If the value is nil, then no converstion will be performed.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/logging/utils.rb', line 22

def getopt( *args )
  opts = args.last.instance_of?(Hash) ? args.pop : {}
  key, default = args

  val = if has_key?(key);                self[key]
        elsif has_key?(key.to_s);        self[key.to_s]
        elsif has_key?(key.to_s.intern); self[key.to_s.intern]
        else default end

  return if val.nil?
  return val unless opts.has_key?(:as)

  case opts[:as].name.intern
  when :Integer; Integer(val)
  when :Float;   Float(val)
  when :Array;   Array(val)
  when :String;  String(val)
  when :Symbol;  String(val).intern
  else val end
end