Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/fedux_org_stdlib/core_ext/hash/list.rb,
lib/fedux_org_stdlib/core_ext/hash/options.rb

Overview

Hash

Instance Method Summary collapse

Instance Method Details

#to_listString

Convert Hash to list

Returns:

  • (String)

    A string representation of hash as list



10
11
12
# File 'lib/fedux_org_stdlib/core_ext/hash/list.rb', line 10

def to_list
  map { |key, value| format('%s: %s', key, value) }.to_list
end

#to_optionsObject

Convert hash to command line options

Examples:

Convert true value


hash = {
  opt1: true
}

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

Convert false value


hash = {
  opt1: false
}

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

Convert other values


hash = {
  opt1: 'string'
}

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

Clean keys


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

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

Escape values


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

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


53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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