Class: Array
- Defined in:
- lib/abstractivator/value_map.rb,
lib/abstractivator/proc_ext.rb,
lib/abstractivator/array_ext.rb
Overview
Like Enumerable#map, except if the receiver is not enumerable, i.e., a single value, then it transforms the single value.
- 2,3].value_map { |x| x * x } => [4, 9
-
2 .value_map { |x| x * x } => 4
Direct Known Subclasses
Instance Method Summary collapse
-
#key ⇒ Object
returns the first element of a 2-element array.
-
#to_proc ⇒ Object
A syntactic hack to get hash values.
-
#value ⇒ Object
returns the second element of a 2-element array.
Instance Method Details
#key ⇒ Object
returns the first element of a 2-element array. useful when dealing with hashes in array form. e.g., pairs.map(&:key)
5 6 7 8 |
# File 'lib/abstractivator/array_ext.rb', line 5 def key size == 2 or raise 'array must contain exactly two elements' first end |
#to_proc ⇒ Object
158 159 160 161 |
# File 'lib/abstractivator/proc_ext.rb', line 158 def to_proc raise 'size must be exactly one' unless size == 1 proc{|x| x[first]} end |
#value ⇒ Object
returns the second element of a 2-element array. useful when dealing with hashes in array form. e.g., pairs.map(&:value)
13 14 15 16 |
# File 'lib/abstractivator/array_ext.rb', line 13 def value size == 2 or raise 'array must contain exactly two elements' last end |