Class: Hash

Inherits:
Object show all
Defined in:
lib/ratch/core_ext/to_console.rb

Instance Method Summary collapse

Instance Method Details

#argumentize(args_field = nil) ⇒ Object Also known as: command_vector

Turn a hash into arguments.

h = { :list => [1,2], :base => "HI" }
h.argumentize #=> [ [], { :list => [1,2], :base => "HI" } ]
h.argumentize(:list) #=> [ [1,2], { :base => "HI" } ]


65
66
67
68
69
70
71
72
73
74
# File 'lib/ratch/core_ext/to_console.rb', line 65

def argumentize(args_field=nil)
  config = dup
  if args_field
    args = [config.delete(args_field)].flatten.compact
  else
    args = []
  end
  args << config
  return args
end

#to_consoleObject

Convert an array into command line parameters. The array is accepted in the format of Ruby method arguments –ie. [arg1, arg2, …, hash]



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ratch/core_ext/to_console.rb', line 42

def to_console
  flags = collect do |f,v|
    m = f.to_s.size == 1 ? '-' : '--'
    case v
    when Array
      v.collect{ |e| "#{m}#{f}='#{e}'" }.join(' ')
    when true
      "#{m}#{f}"
    when false, nil
      ''
    else
      "#{m}#{f}='#{v}'"
    end
  end
  flags.join(" ")
end