Class: Hash

Inherits:
Object show all
Defined in:
lib/amp/server/fancy_http_server.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



548
549
550
# File 'lib/amp/support/support.rb', line 548

def self.with_keys(iterable, value=true)
  iterable.inject({}) {|h, k| h.merge!(k => value) }
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, :[]

#pick(*keys) ⇒ Object

Create a subset of self with keys keys.



554
555
556
# File 'lib/amp/support/support.rb', line 554

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