Class: Array
Instance Method Summary collapse
- #collect_with_index(&block) ⇒ Object
-
#rotate ⇒ Object
Rotate an array Example: arr = %w(a b c d e f) arr2 = arr.clone.rotate.
-
#select_with_hash(conditions = {}) ⇒ Object
Example nodes.select_with_hash(:status=>‘running’).
-
#swap!(a, b) ⇒ Object
Swap elements of an array.
- #wrapping_next(id) ⇒ Object
- #zip_offset(offset = 0) ⇒ Object
Instance Method Details
#collect_with_index(&block) ⇒ Object
7 8 9 |
# File 'lib/core/array.rb', line 7 def collect_with_index &block self.enum_for(:each_with_index).collect &block end |
#rotate ⇒ Object
Rotate an array Example:
arr = %w(a b c d e f)
arr2 = arr.clone.rotate
arr.zip(arr2) =>
[["a", "b"], ["b", "c"], ["c", "d"], ["d", "e"], ["e", "f"], ["f", "a"]]
39 40 41 |
# File 'lib/core/array.rb', line 39 def rotate push shift end |
#select_with_hash(conditions = {}) ⇒ Object
Example nodes.select_with_hash(:status=>‘running’)
12 13 14 15 16 17 18 19 |
# File 'lib/core/array.rb', line 12 def select_with_hash(conditions={}) return self if conditions.empty? select do |node| conditions.any? do |k,v| ( node.has_key?(k) && node[k]==v ) or ( node.respond_to?(k) && node.send(k)==v ) end end end |
#swap!(a, b) ⇒ Object
Swap elements of an array
27 28 29 30 |
# File 'lib/core/array.rb', line 27 def swap!(a,b) self[a], self[b] = self[b], self[a] self end |
#wrapping_next(id) ⇒ Object
21 22 23 24 |
# File 'lib/core/array.rb', line 21 def wrapping_next(id) raise "Element #{id} not in array" unless index(id) index(id) >= size-1 ? at(0) : at(index(id)+1) end |
#zip_offset(offset = 0) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/core/array.rb', line 43 def zip_offset(offset=0) arr2 = self.dup out = [] self.each_with_index do |ele,i| break if i == (self.size - offset) out[i] = [ele, arr2[i+1]] end out end |