Class: Hash

Inherits:
Object show all
Defined in:
lib/amp/server/fancy_http_server.rb,
lib/amp/dependencies/maruku/structures_inspect.rb,
lib/amp/support/support.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.with_keys(iterable, value = true) ⇒ Hash

Given a list of key names, and a specified value, we create a hash with those keys all equal to value. Useful for making true/false tables with speedy lookup.

Parameters:

  • iterable (Enumerable)

    any object with Enumerable mixed in can create a hash.

  • value (Object) (defaults to: true)

    (true) the value to assign each key to in the resultant hash

Returns:

  • (Hash)

    a hash with keys from iterable, all set to value



579
580
581
582
583
584
# File 'lib/amp/support/support.rb', line 579

def self.with_keys(iterable, value=true)
  # we try to be functional when it doesn't fuck us over
  # here, the list of keys will never be ridiculous
  # so we go fuctional
  iterable.inject({}) {|h, k| h[k] = value; h }
end

Instance Method Details

#[](key) ⇒ Hash, Value

Same as #[], but will take regexps and try to match those. This will bug out if you are using regexps as keys

Returns:

  • (Hash, Value)

    will return either a hash (if supplied with a regexp) or whatever it would normally return.



13
14
15
16
17
18
19
20
# File 'lib/amp/server/fancy_http_server.rb', line 13

def [](key)
  case key
  when Regexp
    select {|k, _| k =~ key }.to_hash
  else
    get key
  end
end

#getObject



6
# File 'lib/amp/server/fancy_http_server.rb', line 6

alias_method :get, :[]

#inspect_ordered(a = nil, b = nil) ⇒ Object



48
49
50
51
# File 'lib/amp/dependencies/maruku/structures_inspect.rb', line 48

def inspect_ordered(a=nil,b=nil)
  "{"+keys.map{|x|x.to_s}.sort.map{|x|x.to_sym}.
  map{|k| k.inspect + "=>"+self[k].inspect}.join(',')+"}"
end

#only(*keyz) ⇒ Hash

Construct a new hash with only the specified keyz

Returns:



596
597
598
# File 'lib/amp/support/support.rb', line 596

def only(*keyz)
  keyz.inject({}) {|h, k| h[k] = self[k]; h }
end

#pick(*keys) ⇒ Object

Create a subset of self with keys keys.



588
589
590
# File 'lib/amp/support/support.rb', line 588

def pick(*keys)
  keys.inject({}) {|h, (k, v)| h[k] = v; h }
end