Class: Array
- Inherits:
-
Object
- Object
- Array
- Includes:
- ActiveSupport::CoreExtensions::Array::ExtractOptions
- Defined in:
- lib/imw/utils/extensions/array.rb,
lib/imw/utils/extensions/array.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#dump(uri) ⇒ Object
Dump the data in this array to the resource at the given
uri
. - #in_groups_of(number, fill_with = nil, &block) ⇒ Object
-
#merge_hashes ⇒ Object
Returns a single hash containing the merge of all hashes in this array.
-
#most ⇒ Object
Return all but the last element This will be [] for both an empty array and a length-1 array.
-
#quote_items_with(final_string = nil) ⇒ Object
Return the elements of this array in a pretty-printed string, inserting
final_string
between the last two items. -
#random_element ⇒ Object
Return a random element of this array.
-
#rest ⇒ Object
Return all but the first element.
-
#terminals(&block) ⇒ Object
Recurses through the elements of this Array collecting all String or Symbol “terminal” nodes.
-
#to_openstruct ⇒ Object
convert an assoc (list of [key, val, […]]‘s) to a hash.
-
#unzip ⇒ Object
‘Un’-zip()s an array.
Instance Method Details
#dump(uri) ⇒ Object
Dump the data in this array to the resource at the given uri
.
111 112 113 |
# File 'lib/imw/utils/extensions/array.rb', line 111 def dump uri IMW.open!(uri).dump(self) end |
#in_groups_of(number, fill_with = nil, &block) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/imw/utils/extensions/array.rb', line 77 def in_groups_of(number, fill_with = nil, &block) require 'enumerator' collection = dup collection << fill_with until collection.size.modulo(number).zero? collection.each_slice(number, &block) end |
#merge_hashes ⇒ Object
Returns a single hash containing the merge of all hashes in this array. This is useful when dealing with badly written YAML files. Only merges hashes at depth zero, i.e. - this isn’t recursive.
87 88 89 90 91 92 93 |
# File 'lib/imw/utils/extensions/array.rb', line 87 def merge_hashes merged_hash = {} self.each do |element| merged_hash.merge!(element) if element.is_a?(Hash) end merged_hash end |
#most ⇒ Object
Return all but the last element This will be [] for both an empty array and a length-1 array
10 |
# File 'lib/imw/utils/extensions/array.rb', line 10 def most() self[0..-2] end |
#quote_items_with(final_string = nil) ⇒ Object
Return the elements of this array in a pretty-printed string, inserting final_string
between the last two items.
>> [:one, :two, :three].quote_items_with "or"
`one', `two', or `three'
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/imw/utils/extensions/array.rb', line 53 def quote_items_with final_string = nil string_items = self.map { |item| "`" + item.to_s + "'" } case string_items.length when 0 "" when 1 string_items.first when 2 if final_string then string_items.join(" #{final_string} ") else string_items.join(', ') end else string = string_items[0,string_items.length - 1].join ', ' if final_string then string += ', ' + final_string + ' ' + string_items.last else string += ', ' + string_items.last end string end end |
#random_element ⇒ Object
Return a random element of this array.
36 37 38 |
# File 'lib/imw/utils/extensions/array.rb', line 36 def random_element self[rand(self.length) - 1] end |
#rest ⇒ Object
Return all but the first element. This will be nil for an empty array and [] for a length-1 array
14 |
# File 'lib/imw/utils/extensions/array.rb', line 14 def rest() self[1..-1] end |
#terminals(&block) ⇒ Object
Recurses through the elements of this Array collecting all String or Symbol “terminal” nodes.
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/imw/utils/extensions/array.rb', line 97 def terminals &block terminals = [] each do |element| if element.respond_to? :terminals then terminals += element.terminals else terminals << element end end terminals.map! {|terminal| yield terminal } if block terminals end |
#to_openstruct ⇒ Object
convert an assoc (list of [key, val, […]]‘s) to a hash
41 42 43 44 45 |
# File 'lib/imw/utils/extensions/array.rb', line 41 def to_openstruct mapped = {} each{ |key,value| mapped[key] = value.to_openstruct } OpenStruct.new(mapped) end |
#unzip ⇒ Object
‘Un’-zip()s an array. Returns an array of arrays: the first array has the first element of each member, the second array has the second element of each member, and so on. Returns as many arrays as the first element in self and inserts a nil where the member array wasn’t long enough.
foo, bar = foo.zip(bar).unzip should leave foo and bar with the same values if foo and bar have the same length.
Will fail on a not-array-of-arrays.
25 26 27 28 29 30 31 32 33 |
# File 'lib/imw/utils/extensions/array.rb', line 25 def unzip() # An array of empty arrays, one for each vertical slot vslices = self[0].map{ Array.new } self.each do |hslice| # push the elements of each array onto its slice. vslices.zip(hslice).map{|vslice,h_el| vslice << h_el } end vslices end |