Module: PositiveGroupSupport::ArrayExt

Defined in:
lib/positive_group_support/array_ext.rb

Overview

配列 Array への追加機能をまとめたモジュール

基礎情報 collapse

重複 collapse

Instance Method Details

#cons(n) ⇒ Array

TODO:

説明がしにくい.ドキュメントの重要性を示す例として採用する.

要素を重複ありで n 要素ずつに区切りまとめるメソッド

Examples:

a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

a.cons(2)
  => [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 10]]

a.cons(3)
  => [[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [6, 7, 8], [7, 8, 9], [8, 9, 10]]

fukutoshin_line_weekday_express = [ "Wakoshi" , "Kotake-mukaihara" , "Ikebukuro" , "Shinjuku-sanchome" , "Shibuya" ]
fukutoshin_line_weekday_express.cons(2)
  => [["Wakoshi","Kotake-mukaihara"], ["Kotake-mukaihara", "Ikebukuro"], ["Ikebukuro", "Shinjuku-sanchome"], ["Shinjuku-sanchome", "Shibuya"]]

Returns:

  • (Array)


135
136
137
# File 'lib/positive_group_support/array_ext.rb', line 135

def cons(n)
  each_cons(n).map( &:itself )
end

#digit_of_lengthInteger (natural)

Note:

Numeric#digit を利用する.

長さの桁数を取得する.

Examples:

( 1..99 ).map( &:itself ).digit_of_length => 2
( 1..100 ).map( &:itself ).digit_of_length => 3

Returns:

  • (Integer (natural))


28
29
30
# File 'lib/positive_group_support/array_ext.rb', line 28

def digit_of_length
  length.digit
end

#duplicated?Boolean

重複する要素があることを判定するメソッド

Examples:

[1, 1, 2, 3, 5, 8, 13].duplicated? => true
[1, 2, 4, 6, 8, 10, 12].duplicated? => false

Returns:

  • (Boolean)


50
51
52
# File 'lib/positive_group_support/array_ext.rb', line 50

def duplicated?
  !( uniq? )
end

#duplicated_elementsArray

重複する要素の配列を取得するメソッド

Examples:

[1, 1, 2, 3, 3, 3, 2, 3, 4, 3, 3, 5].duplicated_elements => [1, 2, 3]

Returns:

  • (Array)

See Also:



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_indexInteger (natural)

最後の要素の添字(長さ - 1)を取得する.

Examples:

[0, 1, 2, 3, 4].last_index => 4
[1, 2, 3, 4, 5].last_index => 4
[0].last_index => 0
[].last_index => -1
[0, 1, 2, 3, 4, 5, 6].last_index => 6
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].last_index => 9
[1, 2, 3, 4, 5, 6].last_index => 5
[1, 2, 3, 4, 5, 6, 7, 8, 9].last_index => 8
[2, 4, 6, 8, 10, 12, 14, 16].last_index => 7

Returns:

  • (Integer (natural))


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 であるか否かを判定する.

Examples:

(1..100).map( &:itself ).length?(100) => true
(1..100).map( &:itself ).as_long_as?(100) => true
(1..100).map( &:itself ).length?(10) => false
(1..100).map( &:itself ).length? => true
(1..100).map( &:itself ).length?( nil ) => true

[1, 1, 2, 3, 5, 8, 13].length?(7) => true
[1, 1, 2, 3, 5, 8, 13].as_long_as?(7) => true
[1, 1, 2, 3, 5, 8, 13].length?(13) => false
[1, 1, 2, 3, 5, 8, 13].length? => true
[1, 1, 2, 3, 5, 8, 13].length?( nil ) => true

Parameters:

  • n (Integer (natural) or nil) (defaults to: nil)

    自然数を指定する.nil を指定した場合は常に true を返す.

Returns:

  • (Boolean)

    self の長さが n であるか否かの真偽値

Raises:

  • (Error)

    n に自然数でも nil でもないものを与えたときに発生するエラー



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_improperString or Numeric

Note:

文字列の配列などにも適用できるという点で #sum とは異なる.

self の各要素を + で結合して演算を行なった結果を返す.

Examples:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].sum_improper => 55
( 1..100 ).map( &:itself ).sum_improper => 5050
["a", "b", "c", "d", "e", "f", "g"].sum_improper => "abcdefg"

Returns:

  • (String or Numeric)


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 を比較することで判定する.

重複する要素がないことを判定するメソッド

Examples:

[1, 1, 2, 3, 5, 8, 13].uniq? => false
[1, 2, 4, 6, 8, 10, 12].uniq? => true

Returns:

  • (Boolean)

See Also:



41
42
43
# File 'lib/positive_group_support/array_ext.rb', line 41

def uniq?
  length == uniq.length
end

#uniqueArray

TODO:

説明がしにくい.ドキュメントの重要性を示す例として採用する.

配列内で連続して同じ要素が続いている場合,連続部分での重複を解消する.

Examples:

[1, 1, 2, 3, 3, 3, 2, 3, 4, 3, 3, 5].unique => [1, 2, 3, 2, 3, 4, 3, 5]

 cf. Array#uniq との違い
[1, 1, 2, 3, 3, 3, 2, 3, 4, 3, 3, 5].uniq => [1, 2, 3, 4, 5]

Returns:

  • (Array)


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