Parent

Files

Stash

Stash is just like Hash, except that all keys are converted to Strings.

Note this doesn't yet handle default_proc.

Public Class Methods

[](*hash) click to toggle source
# File lib/hashery/stash.rb, line 9
def self.[](*hash)
  s = new
  super(*hash).each{ |k,v| s[k] = v }
  s
end

Public Instance Methods

<<(other) click to toggle source
# File lib/hashery/stash.rb, line 26
def <<(other)
  case other
  when Hash
    super(other.rekey{ |key| convert_key(key) })
  when Array
    self[other[0]] = other[1]
  else
    raise ArgumentError
  end
end
[](key) click to toggle source
# File lib/hashery/stash.rb, line 16
def [](key)
  super(convert_key(key))
end
[]=(key,value) click to toggle source
# File lib/hashery/stash.rb, line 21
def []=(key,value)
  super(convert_key(key), value)
end
delete(key) click to toggle source
# File lib/hashery/stash.rb, line 97
def delete(key)
  super(convert_key(key))
end
fetch(key) click to toggle source
# File lib/hashery/stash.rb, line 38
def fetch(key)
  super(convert_key(key))
end
has_key?(key) click to toggle source
# File lib/hashery/stash.rb, line 53
def has_key?(key)
  super(convert_key(key))
end
include?(key) click to toggle source
# File lib/hashery/stash.rb, line 58
def include?(key)
  super(convert_key(key))
end
key?(key) click to toggle source
# File lib/hashery/stash.rb, line 48
def key?(key)
  super(convert_key(key))
end
member?(key) click to toggle source
# File lib/hashery/stash.rb, line 63
def member?(key)
  super(convert_key(key))
end
merge(other) click to toggle source
# File lib/hashery/stash.rb, line 112
def merge(other)
  super(other.rekey{ |key| convert_key(key) })
end
merge!(other) click to toggle source

Same as update.

# File lib/hashery/stash.rb, line 107
def merge!(other)
  super(other.rekey{ |key| convert_key(key) })
end
rekey(*args, &block) click to toggle source
# File lib/hashery/stash.rb, line 92
def rekey(*args, &block)
  dup.rekey!(*args, &block)
end
rekey!(*args, &block) click to toggle source

Synonym for Hash#rekey, but modifies the receiver in place (and returns it).

foo = { :name=>'Gavin', :wife=>:Lisa }.to_stash
foo.rekey!{ |k| k.upcase }  #=>  { "NAME"=>"Gavin", "WIFE"=>:Lisa }
foo.inspect                 #=>  { "NAME"=>"Gavin", "WIFE"=>:Lisa }
# File lib/hashery/stash.rb, line 74
def rekey!(*args, &block)
  # for backward comptability (TODO: DEPRECATE?).
  block = args.pop.to_sym.to_proc if args.size == 1
  if args.empty?
    block = lambda{|k| k} unless block
    keys.each do |k|
      nk = block[k]
      self[nk.to_s]=delete(k) #if nk
    end
  else
    raise ArgumentError, "3 for 2" if block
    to, from = *args
    self[to] = delete(from) if has_key?(from)
  end
  self
end
replace(other) click to toggle source
# File lib/hashery/stash.rb, line 117
def replace(other)
  super(other.rekey{ |key| convert_key(key) })
end
store(key, value) click to toggle source
# File lib/hashery/stash.rb, line 43
def store(key, value)
  super(convert_key(key), value)
end
to_h() click to toggle source
Alias for: to_hash
to_hash() click to toggle source
# File lib/hashery/stash.rb, line 127
def to_hash
  h = {}
  each{ |k,v| h[k] = v }
  h
end
Also aliased as: to_h
update(other) click to toggle source
# File lib/hashery/stash.rb, line 102
def update(other)
  super(other.rekey{ |key| convert_key(key) })
end
values_at(*keys) click to toggle source
# File lib/hashery/stash.rb, line 122
def values_at(*keys)
  super(*keys.map{ |key| convert_key(key) })
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.