Method: Array#fetch_opts
- Defined in:
- lib/rmtools/core/arguments.rb
#fetch_opts(defaults = [], opts = {}) ⇒ Object Also known as: get_opts
a, b, opts = [<hash1>].fetch_opts([<hash2>, <object1>]) may work unintuitive: you’ll get <hash1> as ‘a’ and not as ‘opts’ So if function is not implying that some argument other than ‘opts’ definetly must be a hash, don’t make hash default. Better put hashie argument into opts like b, opts = [<hash1>].fetch([<object1>], :a => <hash2>) and get ‘a’ from ‘’opts’ hash
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rmtools/core/arguments.rb', line 11 def fetch_opts(defaults=[], opts={}) if Hash === defaults opts, defaults = defaults, [] return_hash = true $log.warn "fetch_opts(<hash>) now changed, if you want to jsut fetch hash options, use `opts = <hash>.merge(opts||{})' construction", :caller => 2 else return_hash = false end opts &&= if Hash === self[-1] and !(Hash === defaults[size-1]) opts.merge pop else opts.dup end return opts if return_hash if defaults == :flags defaults = [:flags] end if defaults.last == :flags defaults.pop flags = defaults.size..-1 if defaults.size < size self[flags].each {|flag| opts[flag] = true} self[flags] = [] end end each_index {|i| import(defaults, i) if :def == self[i]} defaults.slice! 0, size concat defaults << opts end |