class Hash

Hash

Hash

Public Instance Methods

to_list(format: '%s: %s', **args) click to toggle source

Convert Hash to list

For more examples see 'fedux_org_stdlib/core_ext/array/list'

@example Change separator for key and value

input = {
  opt1: 'asdf',
  opt2: 'asdf'
}

input.to_list(format: "%s | %s")
=> "opt1 | asdf", "opt2 | asdf"

@example Change separator between key/value-pairs

input = {
  opt1: 'asdf',
  opt2: 'asdf'
}

input.to_list(format: "%s | %s")
=> "opt1 | asdf", "opt2 | asdf"

@example Change character around key/value-pair

input = {
  opt1: 'asdf',
  opt2: 'asdf'
}

input.to_list(around: "'")
=> 'opt1: asdf', 'opt2: asdf'

@return [String]

A string representation of hash as list
# File lib/fedux_org_stdlib/core_ext/hash/list.rb, line 42
def to_list(format: '%s: %s', **args)
  map { |key, value| format(format, key, value) }.to_list(**args)
end
to_options() click to toggle source

Convert hash to command line options

@example Convert true value

hash = {
  opt1: true
}

hash.to_options
# => [ '--opt1']

@example Convert false value

hash = {
  opt1: false
}

hash.to_options
# => [ '--no-opt1']

@example Convert other values

hash = {
  opt1: 'string'
}

hash.to_options
# => [ '--opt1', 'string']

@example Clean keys

hash = {
  '$opt1' => 'string'
}

hash.to_options
# => [ '--opt1', 'string']

@example Escape values

hash = {
  'opt1' => '$string'
}

hash.to_options
# => [ '--opt1', '\$string']
# File lib/fedux_org_stdlib/core_ext/hash/options.rb, line 53
def to_options
  each_with_object([]) do |(key, value), a|
    key = Shellwords.clean(key)

    if value.is_a? TrueClass
      a << "--#{key}"
    elsif value.is_a? FalseClass
      a << "--no-#{key}"
    else
      a << "--#{key}"
      a << Shellwords.escape(value)
    end
  end
end