class Array

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

Public Instance Methods

key() click to toggle source

returns the first element of a 2-element array. useful when dealing with hashes in array form. e.g., pairs.map(&:key)

# File lib/abstractivator/array_ext.rb, line 5
def key
  size == 2 or raise 'array must contain exactly two elements'
  first
end
to_proc() click to toggle source

A syntactic hack to get hash values. xs.map(&:name) works when xs is an array of objects, each with a name method. (built into ruby) xs.map(&) works when xs is an array of hashes, each with a :name key. xs.map(&) works when xs is an array of hashes, each with a 'name' key.

# 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() click to toggle source

returns the second element of a 2-element array. useful when dealing with hashes in array form. e.g., pairs.map(&:value)

# File lib/abstractivator/array_ext.rb, line 13
def value
  size == 2 or raise 'array must contain exactly two elements'
  last
end