Module: FatCore::Array
- Included in:
- Array
- Defined in:
- lib/fat_core/array.rb
Overview
Useful extensions to the core Array class.
Instance Method Summary collapse
-
#comma_join(sep: nil, last_sep: nil, two_sep: nil) ⇒ Object
Convert this array into a single string by (1) applying #to_s to each element and (2) joining the elements with the string given by the sep: parameter.
-
#diff_with_dups(*others) ⇒ Object
Return an Array that is the difference between this Array and +other+, but without removing duplicates as the Array#- method does.
-
#intersect_with_dups(*others) ⇒ Object
Return a new Array that is the intersection of this Array with all +others+, but without removing duplicates as the Array#& method does.
-
#last_i ⇒ Object
Return the index of the last element of this Array.
Instance Method Details
#comma_join(sep: nil, last_sep: nil, two_sep: nil) ⇒ Object
Convert this array into a single string by (1) applying #to_s to each element and (2) joining the elements with the string given by the sep: parameter. By default the sep parameter is ', '. You may use a different separation string in the case when there are only two items in the list by supplying a two_sep parameter. You may also supply a difference separation string to separate the second-last and last items in the array by supplying a last_sep: parameter. By default, the sep parameter is the string ', ', the two_sep is ' and ', and the last_sep is ', and ', all of which makes for a well-punctuated English clause. If sep is given, the other two parameters are set to its value by default. If last_sep is given, two_sep takes its value by default. If the input array is empty, #comma_join returns an empty string.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/fat_core/array.rb', line 48 def comma_join(sep: nil, last_sep: nil, two_sep: nil) orig_sep = sep orig_last_sep = last_sep sep ||= ', ' last_sep ||= orig_sep || ', and ' two_sep ||= orig_sep || orig_last_sep || ' and ' result = +'' case size when 0 result when 1 result = self[0].to_s when 2 result = self[0].to_s + two_sep + self[1] else second_last = size - 2 last = size - 1 each_with_index do |itm, k| result << if k == second_last "#{itm}#{last_sep}" elsif k == last itm.to_s else "#{itm}#{sep}" end end end result end |
#diff_with_dups(*others) ⇒ Object
Return an Array that is the difference between this Array and +other+, but without removing duplicates as the Array#- method does. All items of this Array are included in the result unless they also appear in any of the +other+ Arrays.
28 29 30 31 32 33 34 |
# File 'lib/fat_core/array.rb', line 28 def diff_with_dups(*others) result = [] each do |itm| result << itm if others.none? { |oth| oth.include?(itm) } end result end |
#intersect_with_dups(*others) ⇒ Object
Return a new Array that is the intersection of this Array with all +others+, but without removing duplicates as the Array#& method does. All items of this Array are included in the result but only if they also appear in all of the other Arrays.
16 17 18 19 20 21 22 |
# File 'lib/fat_core/array.rb', line 16 def intersect_with_dups(*others) result = [] each do |itm| result << itm if others.all? { |oth| oth.include?(itm) } end result end |
#last_i ⇒ Object
Return the index of the last element of this Array. This is just a convenience for an oft-needed Array attribute.
8 9 10 |
# File 'lib/fat_core/array.rb', line 8 def last_i size - 1 end |