Class: OHash

Inherits:
Hash show all
Includes:
OHashMixIn
Defined in:
lib/o_hash.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OHashMixIn

#==, #[]=, #clear, #delete, #delete_if, #each, #each_key, #each_value, #inspect, #invert, #keys, #merge, #merge!, #ordered?, #pretty_print, #pretty_print_cycle, #reject, #reject!, #replace, #select, #shift, #store, #to_a, #to_s, #update, #values

Methods inherited from Hash

#choose, #no_diff?, #ordered?, #regex_path_match, #symtbl_gsub, #symtbl_gsub!, #yaml_doc_traverse

Methods included from PpHierarchy

#pp_hierarchy

Methods included from Diff

#diff, #gen_diff, #no_diff?

Constructor Details

#initialize(*a) ⇒ OHash

Returns a new instance of OHash.



174
175
176
177
# File 'lib/o_hash.rb', line 174

def initialize ( *a )
  @order = unordered_keys
  super
end

Instance Attribute Details

#orderObject

Returns the value of attribute order.



168
169
170
# File 'lib/o_hash.rb', line 168

def order
  @order
end

Class Method Details

.[](*args) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/o_hash.rb', line 179

def self.[] ( *args )
  hsh = OHash.new
  if Hash === args[0]
    hsh.replace args[0]
  elsif (args.size % 2) != 0
    raise ArgumentError, "odd number of elements for Hash"
  else
    hsh[args.shift] = args.shift while args.size > 0
  end
  hsh
end

.yaml_load(val) ⇒ Object

Raises:

  • (ArgumentError)


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/o_hash.rb', line 193

def self.yaml_load ( val )
  case val
  when Hash
    return OHash[val.to_a]
  when OHash
    return val
  when Array
    if val.all? { |x| x.is_a?(Hash) and x.size == 1 }
      h = OHash.new
      val.each do |x|
        k, v = x
        h[k] = v
      end
      return h
    end
  end
  raise ArgumentError, "Cannot create a OHash with `#{val}'"
end