Sha256: c98a6349dca2bd6d7c5e4fe3fc27e9c1288ea61ef25a2a8b0ff6663394488ea4
Contents?: true
Size: 1.1 KB
Versions: 4
Compression:
Stored size: 1.1 KB
Contents
# $Id: utils.rb 65 2007-12-23 04:48:55Z tim_pease $ class Hash # 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. # def getopt( *args ) opts = Hash === args.last ? 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 end # EOF
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
logging-0.6.1 | lib/logging/utils.rb |
logging-0.6.0 | lib/logging/utils.rb |
logging-0.6.2 | lib/logging/utils.rb |
logging-0.6.3 | lib/logging/utils.rb |