Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/arrays.rb
Overview
Array extension
Instance Method Summary collapse
-
#check_empty_list ⇒ Object
throw exception if list is empty.
-
#cluster(&block) ⇒ Object
cluster element with keep order accept binary predicate function [1, 2, 2, 2].cluster { |a, b| a == b } => [[1], [2, 2, 2]].
-
#init ⇒ Object
drop last one element [1, 2].tail => [2].
-
#tail ⇒ Object
drop one element [1, 2].tail => [2].
Instance Method Details
#check_empty_list ⇒ Object
throw exception if list is empty
25 26 27 |
# File 'lib/arrays.rb', line 25 def check_empty_list throw ":empty list" if self.empty? end |
#cluster(&block) ⇒ Object
cluster element with keep order accept binary predicate function [1, 2, 2, 2].cluster { |a, b| a == b }
> [[1], [2, 2, 2]]
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/arrays.rb', line 9 def cluster &block self.inject([]) do |acc, item| if acc.empty? [[item]] else if block.call(acc[-1][-1], item) acc[-1] << item else acc << [item] end acc end end end |
#init ⇒ Object
drop last one element [1, 2].tail
> [2]
32 33 34 35 |
# File 'lib/arrays.rb', line 32 def init check_empty_list self.take(self.length-1) end |
#tail ⇒ Object
drop one element [1, 2].tail
> [2]
40 41 42 43 |
# File 'lib/arrays.rb', line 40 def tail check_empty_list self.drop(1) end |