class Hash

Public Instance Methods

shelljoin() click to toggle source
# File lib/facets/shellwords.rb, line 107
def shelljoin
  shellwords.shelljoin
end
shellwords() click to toggle source
# File lib/facets/shellwords.rb, line 87
def shellwords
  argv = []
  each do |f,v|
    m = f.to_s.size == 1 ? '-' : '--'
    case v
    when false, nil
    when Array
      v.each do |e|
        argv << %[#{m}#{f}="#{e}"]
      end
    when true
      argv << %[#{m}#{f}]
    else
      argv << %[#{m}#{f}="#{v}"]
    end
  end
  argv
end
to_ostruct() click to toggle source

Turns a hash into a generic object using an OpenStruct.

o = {'a' => 1}.to_ostruct
o.a  #=> 1
# File lib/facets/ostruct.rb, line 172
def to_ostruct
  OpenStruct.new(self)
end
to_ostruct_recurse(exclude={}) click to toggle source

Like #to_ostruct but recusively objectifies all hash elements as well.

o = {'a' => { 'b' => 1 }}.to_ostruct_recurse
o.a.b  #=> 1

The exclude parameter is used internally to prevent infinite recursion and is not intended to be utilized by the end-user. But for more advance use, if there is a particular subhash you would like to prevent from being converted to an OpoenStruct then include it in the exclude hash referencing itself. Eg.

h = { 'a' => { 'b' => 1 } }
o = h.to_ostruct_recurse( { h['a'] => h['a'] } )
o.a['b']  #=> 1

CREDIT: Alison Rowland, Jamie Macey, Mat Schaffer

# File lib/facets/ostruct.rb, line 192
def to_ostruct_recurse(exclude={})
  return exclude[self] if exclude.key?( self )
  o = exclude[self] = OpenStruct.new
  h = self.dup
  each_pair do |k,v|
    h[k] = v.to_ostruct_recurse( exclude ) if v.respond_to?(:to_ostruct_recurse)
  end
  o.__update__(h)
end