Class: Hash

Inherits:
Object show all
Defined in:
lib/corelib_ruby/core_ext/hash/core.rb,
lib/corelib_ruby/core_ext/hash/extract.rb,
lib/corelib_ruby/core_ext/hash/iterators.rb

Instance Method Summary collapse

Instance Method Details

#cl_add_if_absent(key, value) ⇒ Object

Add an key value if the key doesn’t already exist. It will return the currently set value if the key exists, or the new value if the key doesn’t exist.



10
11
12
13
# File 'lib/corelib_ruby/core_ext/hash/core.rb', line 10

def cl_add_if_absent(key, value)
  current = self[key]
  current.nil? ? (self[key] = value) : current
end

#cl_each_with_last_flagObject

Tterates over the Array just as you would with Array#each. However, for each iteration pass in an extra variable that will be ‘false` until the last iteration.



4
5
6
7
8
9
10
11
# File 'lib/corelib_ruby/core_ext/hash/iterators.rb', line 4

def cl_each_with_last_flag
  my_size = size
  index = 1
  each_pair do |key, value|
    yield(key, value, index == my_size)
    index += 1
  end
end

#cl_extract(*args) ⇒ Object

This method mimics the fetch method except it also deletes the key from the hash once it has been fetched.

If key exists in the hash, it’s value will be returned. If key doesn’t exist, and optional default value will be returned If key doesn’t exist and no default is specified, a KeyError is raised If key doesn’t exists, there is no default, but a block is given, then that block will be executed with the key.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/corelib_ruby/core_ext/hash/extract.rb', line 10

def cl_extract(*args)
  arg_length = args.length
  fail ArgumentError, "wrong number of arguments #{arg_length} for 1..2" if arg_length == 0 || arg_length > 2
  key = args[0]

  # Just return the value if the key exists.
  return delete(key) if key?(key)

  # The key doesn't exist.  If the user gave us a block, yield to it and return the value from the block.
  return yield key if block_given?
  (args.length == 2) ? args[1] : fail(KeyError, "key not found #{key.inspect}", caller)
end

#cl_fetch_ignoring_nil(key, default) ⇒ Object

This method works similiar to fetch, however it handles the case where a key is already present in the hash, but has a value of nil. This method will return the default instead of returning the nil.



24
25
26
27
# File 'lib/corelib_ruby/core_ext/hash/core.rb', line 24

def cl_fetch_ignoring_nil(key, default)
  val = fetch(key, default)
  val.nil? ? default : val
end

#cl_not_empty?Boolean

A simple helper to improve readability. [].cl_not_empty? reads better than ![].empty?

Returns:

  • (Boolean)


4
5
6
# File 'lib/corelib_ruby/core_ext/hash/core.rb', line 4

def cl_not_empty?
  !empty?
end

#cl_update_if_present(key, value) ⇒ Object

Update a key’s value if the key exists. It will return the new value if an update was made, or nil if the key doesn’t exist.



17
18
19
20
# File 'lib/corelib_ruby/core_ext/hash/core.rb', line 17

def cl_update_if_present(key, value)
  current = self[key]
  current.nil? ? nil : (self[key] = value)
end