Class: Rbkb::Http::NamedValueArray

Inherits:
Array
  • Object
show all
Defined in:
lib/rbkb/http/common.rb

Overview

A generic cheat for an Array of named value pairs to pretend to be like Hash when using [] and []=

Instance Method Summary collapse

Instance Method Details

#[](*args) ⇒ Object

Act like a hash with named values. Return the named value if a string or Symbol is supplied as the index argument.

Note, this doesn’t do any magic with String / Symbol conversion.



43
44
45
46
47
48
49
50
51
# File 'lib/rbkb/http/common.rb', line 43

def [](*args)
  if args.size == 1 and (String === args[0] or Symbol === args[0])
    if h=find {|x| x[0] == args[0]}
      return h[1]
    end
  else
    super(*args)
  end
end

#[]=(*args) ⇒ Object

Act like a hash with named values. Set the named value if a String or Symbol is supplied as the index argument.

Note, this doesn’t do any magic with String / Symbol conversion.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rbkb/http/common.rb', line 57

def []=(*args)
  if args.size > 1 and (String === args[0] or Symbol === args[0])
    if h=find {|x| x[0] == args[0]}
      h[1] = args[1]
    else
      self << args[0,2]
    end
  else
    super(*args)
  end
end

#delete_key(key) ⇒ Object



69
70
71
# File 'lib/rbkb/http/common.rb', line 69

def delete_key(key)
  delete_if {|x| x[0] == key }
end