Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/imw/utils/extensions/array.rb
Overview
class Array #:nodoc:
include ActiveSupport::CoreExtensions::Array::ExtractOptions
end
Direct Known Subclasses
Instance Method Summary collapse
-
#emit(uri) ⇒ Object
Emit 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
#emit(uri) ⇒ Object
Emit the data in this array to the resource at the given uri
.
110 111 112 |
# File 'lib/imw/utils/extensions/array.rb', line 110 def emit uri IMW.open!(uri).emit(self) end |
#in_groups_of(number, fill_with = nil, &block) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/imw/utils/extensions/array.rb', line 76 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.
86 87 88 89 90 91 92 |
# File 'lib/imw/utils/extensions/array.rb', line 86 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
9 |
# File 'lib/imw/utils/extensions/array.rb', line 9 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'
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/imw/utils/extensions/array.rb', line 52 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.
35 36 37 |
# File 'lib/imw/utils/extensions/array.rb', line 35 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
13 |
# File 'lib/imw/utils/extensions/array.rb', line 13 def rest() self[1..-1] end |
#terminals(&block) ⇒ Object
Recurses through the elements of this Array collecting all String or Symbol “terminal” nodes.
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/imw/utils/extensions/array.rb', line 96 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
40 41 42 43 44 |
# File 'lib/imw/utils/extensions/array.rb', line 40 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.
24 25 26 27 28 29 30 31 32 |
# File 'lib/imw/utils/extensions/array.rb', line 24 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 |