Module: PositiveGroupSupport::ArrayExt
- Defined in:
- lib/positive_group_support/array_ext.rb
Overview
配列 Array への追加機能をまとめたモジュール
基礎情報 collapse
-
#digit_of_length ⇒ Integer (natural)
長さの桁数を取得する..
-
#last_index ⇒ Integer (natural)
最後の要素の添字(長さ - 1)を取得する..
重複 collapse
-
#cons(n) ⇒ Array
要素を重複ありで n 要素ずつに区切りまとめるメソッド.
-
#duplicated? ⇒ Boolean
重複する要素があることを判定するメソッド.
-
#duplicated_elements ⇒ Array
重複する要素の配列を取得するメソッド.
-
#length?(n = nil) ⇒ Boolean
(also: #as_long_as?)
長さが n であるか否かを判定する..
-
#sum_improper ⇒ String or Numeric
self の各要素を + で結合して演算を行なった結果を返す..
-
#uniq? ⇒ Boolean
重複する要素がないことを判定するメソッド.
-
#unique ⇒ Array
配列内で連続して同じ要素が続いている場合,連続部分での重複を解消する..
Instance Method Details
#cons(n) ⇒ Array
TODO:
説明がしにくい.ドキュメントの重要性を示す例として採用する.
要素を重複ありで n 要素ずつに区切りまとめるメソッド
135 136 137 |
# File 'lib/positive_group_support/array_ext.rb', line 135 def cons(n) each_cons(n).map( &:itself ) end |
#digit_of_length ⇒ Integer (natural)
Note:
Numeric#digit を利用する.
長さの桁数を取得する.
28 29 30 |
# File 'lib/positive_group_support/array_ext.rb', line 28 def digit_of_length length.digit end |
#duplicated? ⇒ Boolean
重複する要素があることを判定するメソッド
50 51 52 |
# File 'lib/positive_group_support/array_ext.rb', line 50 def duplicated? !( uniq? ) end |
#duplicated_elements ⇒ Array
重複する要素の配列を取得するメソッド
59 60 61 |
# File 'lib/positive_group_support/array_ext.rb', line 59 def duplicated_elements uniq.select{ |i| index(i) != rindex(i) } end |
#last_index ⇒ Integer (natural)
最後の要素の添字(長さ - 1)を取得する.
18 19 20 |
# File 'lib/positive_group_support/array_ext.rb', line 18 def last_index length - 1 end |
#length?(n = nil) ⇒ Boolean Also known as: as_long_as?
長さが n であるか否かを判定する.
97 98 99 100 101 102 103 104 105 |
# File 'lib/positive_group_support/array_ext.rb', line 97 def length?(n=nil) if n.nil? true elsif n.natural_number? length == n else raise "Error: The variable should be a natural number or \"nil\"." end end |
#sum_improper ⇒ String or Numeric
Note:
文字列の配列などにも適用できるという点で #sum とは異なる.
self の各要素を + で結合して演算を行なった結果を返す.
116 117 118 |
# File 'lib/positive_group_support/array_ext.rb', line 116 def sum_improper self.inject(:+) end |
#uniq? ⇒ Boolean
Note:
self.length と self.uniq.length を比較することで判定する.
重複する要素がないことを判定するメソッド
41 42 43 |
# File 'lib/positive_group_support/array_ext.rb', line 41 def uniq? length == uniq.length end |
#unique ⇒ Array
TODO:
説明がしにくい.ドキュメントの重要性を示す例として採用する.
配列内で連続して同じ要素が続いている場合,連続部分での重複を解消する.
71 72 73 74 75 76 77 78 79 |
# File 'lib/positive_group_support/array_ext.rb', line 71 def unique ary = ::Array.new each_with_index{ | element , i | unless i > 0 && self[i-1] == element ary << element end } ary end |