Class: Array
- Defined in:
- lib/patch/let.rb,
lib/patch/foobar.rb,
lib/patch/lambda.rb
Overview
Monkey patches for use in Lambda
Instance Method Summary collapse
-
#hashy? ⇒ Boolean
Checks if the array is “hashy”, meaning it consists entirely of two-element arrays.
-
#op(method) ⇒ Object
Allows for this: - ‘[1, 2, 3].op(:+) # => 6` - `[1, 2, 3].op(:-) # => -4`.
-
#qoq(default = nil) ⇒ Object
Same as [Array#pop] but returns
defaultif the array is empty. -
#rest ⇒ Array
Returns the rest of the array (all elements except the first).
-
#simplify ⇒ Object, Array
Simplifies the array: if it contains exactly one element, returns that element.
Instance Method Details
#hashy? ⇒ Boolean
Checks if the array is “hashy”, meaning it consists entirely of two-element arrays. This structure is suitable for conversion to a Hash using to_h.
40 41 42 |
# File 'lib/patch/let.rb', line 40 def hashy? all? { |item| item.is_a?(Array) && item.size == 2 } end |
#op(method) ⇒ Object
Allows for this:
-
‘[1, 2, 3].op(:+) # => 6`
-
‘[1, 2, 3].op(:-) # => -4`
Not the same as [reduce] or [inject] The method argument is supposed to be an instance method for the first element of the array.
12 13 14 |
# File 'lib/patch/lambda.rb', line 12 def op(method) first.send(method, slice(1..)) end |
#qoq(default = nil) ⇒ Object
Same as [Array#pop] but returns default if the array is empty.
17 18 19 |
# File 'lib/patch/lambda.rb', line 17 def qoq(default = nil) empty? ? default : pop end |
#rest ⇒ Array
Returns the rest of the array (all elements except the first). Returns an empty array if the original array has 0 or 1 element.
252 253 254 |
# File 'lib/patch/foobar.rb', line 252 def rest drop 1 end |