Module: Receiver::ActsAsHash::InstanceMethods

Defined in:
lib/gorillib/receiver/acts_as_hash.rb

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object

Hashlike#[]

Element Reference – Retrieves the value stored for key.

In a normal hash, a default value can be set; none is provided here.

Delegates to self.send(key)

Examples:

hsh = { :a => 100, :b => 200 }
hsh[:a] # => 100
hsh[:c] # => nil

Parameters:

  • key (Object)

    key to retrieve

Returns:

  • (Object)

    the value stored for key, nil if missing



53
54
55
56
# File 'lib/gorillib/receiver/acts_as_hash.rb', line 53

def [](key)
  key = convert_key(key)
  self.send(key)
end

#[]=(key, val) ⇒ Object

Hashlike#[]= Hashlike#store

Element Assignment – Associates the value given by val with the key given by key.

key should not have its value changed while it is in use as a key. In a normal hash, a String passed as a key will be duplicated and frozen. No such guarantee is provided here

Delegates to self.send(“key=”, val)

Examples:

hsh = { :a => 100, :b => 200 }
hsh[:a] = 9
hsh[:c] = 4
hsh    # => { :a => 9, :b => 200, :c => 4 }

hsh[key] = val                         -> val
hsh.store(key, val)                    -> val

Parameters:

  • key (Object)

    key to associate

  • val (Object)

    value to associate it with

Returns:



83
84
85
86
# File 'lib/gorillib/receiver/acts_as_hash.rb', line 83

def []=(key, val)
  key = convert_key(key)
  self.send("#{key}=", val)
end

#hsh.delete(key) ⇒ Object, Nil #hsh.delete(key) {|Object| ... } ⇒ Object, Nil

Hashlike#delete

Deletes and returns the value from hsh whose key is equal to key. If the optional code block is given and the key is not found, pass in the key and return the result of block.

In a normal hash, a default value can be set; none is provided here.

Examples:

hsh = { :a => 100, :b => 200 }
hsh.delete(:a)                            # => 100
hsh.delete(:z)                            # => nil
hsh.delete(:z){|el| "#{el} not found" }   # => "z not found"

Overloads:

  • #hsh.delete(key) ⇒ Object, Nil

    Returns the removed object, nil if missing.

    Parameters:

    • key (Object)

      key to remove

    Returns:

    • (Object, Nil)

      the removed object, nil if missing

  • #hsh.delete(key) {|Object| ... } ⇒ Object, Nil

    Returns the removed object, or if missing, the return value of the block.

    Parameters:

    • key (Object)

      key to remove

    Yields:

    • (Object)

      called (with key) if key is missing

    Yield Parameters:

    • key

    Returns:

    • (Object, Nil)

      the removed object, or if missing, the return value of the block



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/gorillib/receiver/acts_as_hash.rb', line 113

def delete(key, &block)
  key = convert_key(key)
  if has_key?(key)
    val = self[key]
    self.send(:remove_instance_variable, "@#{key}")
    val
  elsif block_given?
    block.call(key)
  else
    nil
  end
end

#keysArray

Hashlike#keys

Returns a new array populated with the keys from this hashlike.

Examples:

hsh = { :a => 100, :b => 200, :c => 300, :d => 400 }
hsh.keys   # => [:a, :b, :c, :d]

Returns:

  • (Array)

    list of keys

See Also:

  • Hashlike#values.


138
139
140
# File 'lib/gorillib/receiver/acts_as_hash.rb', line 138

def keys
  members & instance_variables.map{|s| convert_key(s[1..-1]) }
end

#membersObject



142
143
144
# File 'lib/gorillib/receiver/acts_as_hash.rb', line 142

def members
  self.class.members
end

#to_hashHash

Returns a hash with each key set to its associated value.

Examples:

my_hshlike = MyHashlike.new
my_hshlike[:a] = 100; my_hshlike[:b] = 200
my_hshlike.to_hash # => { :a => 100, :b => 200 }

Returns:

  • (Hash)

    a new Hash instance, with each key set to its associated value.



156
157
158
159
160
161
162
# File 'lib/gorillib/receiver/acts_as_hash.rb', line 156

def to_hash
  {}.tap do |hsh|
    each_pair do |key, val|
      hsh[key] = val.respond_to?(:to_hash) ? val.to_hash : val
    end
  end
end