Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/hash-polyfill.rb
Instance Method Summary collapse
- #compact ⇒ Object
- #compact! ⇒ Object
- #reject_values ⇒ Object
- #reject_values! ⇒ Object
- #select_values ⇒ Object
- #select_values! ⇒ Object
- #slice(*values) ⇒ Object
- #transform_keys ⇒ Object
- #transform_values ⇒ Object
- #transform_values! ⇒ Object
Instance Method Details
#compact ⇒ Object
2 3 4 5 6 |
# File 'lib/hash-polyfill.rb', line 2 def compact reject do |key, value| value.nil? end end |
#compact! ⇒ Object
8 9 10 11 12 |
# File 'lib/hash-polyfill.rb', line 8 def compact! reject! do |key, value| value.nil? end end |
#reject_values ⇒ Object
28 29 30 31 32 33 |
# File 'lib/hash-polyfill.rb', line 28 def reject_values return enum_for(:reject_values) unless block_given? reject do |key, value| yield(value) end end |
#reject_values! ⇒ Object
35 36 37 38 39 40 |
# File 'lib/hash-polyfill.rb', line 35 def reject_values! return enum_for(:reject_values!) unless block_given? reject! do |key, value| yield(value) end end |
#select_values ⇒ Object
14 15 16 17 18 19 |
# File 'lib/hash-polyfill.rb', line 14 def select_values return enum_for(:select_values) unless block_given? select do |key, value| yield(value) end end |
#select_values! ⇒ Object
21 22 23 24 25 26 |
# File 'lib/hash-polyfill.rb', line 21 def select_values! return enum_for(:select_values!) unless block_given? select! do |key, value| yield(value) end end |
#slice(*values) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/hash-polyfill.rb', line 59 def slice(*values) result = {} values.each do |key| result[key] = self[key] if has_key?(key) end result end |
#transform_keys ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/hash-polyfill.rb', line 67 def transform_keys return enum_for(:transform_keys) unless block_given? result = {} each do |key, value| result[yield(key)] = value end result end |
#transform_values ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/hash-polyfill.rb', line 42 def transform_values return enum_for(:transform_values) unless block_given? result = {} each do |key, value| result[key] = yield(value) end result end |
#transform_values! ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/hash-polyfill.rb', line 51 def transform_values! return enum_for(:transform_values!) unless block_given? each do |key, value| self[key] = yield(value) end self end |