Class: Array

Inherits:
Object show all
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

Abstractivator::Cons::Cell

Instance Method Summary collapse

Instance Method Details

#keyObject

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_procObject

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.



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

#valueObject

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