Module: RubyExtension::Array::InstanceMethods
- Defined in:
- lib/ruby_extension/array.rb
Overview
module ClassMethods # :nodoc: end
Instance Method Summary collapse
-
#arrange(new_array_index = []) ⇒ Object
[‘a’,‘b’,‘c’].arrange() => [‘c’,‘a’,‘b’].
-
#average ⇒ Object
Return the average digitized value of the array.
-
#capitalize ⇒ Object
Return capitlized versions of each item in the array.
-
#capitalize! ⇒ Object
Capitalize each item in the array and return it.
-
#downcase ⇒ Object
Return downcased versions of each item in the array.
-
#drop_blanks! ⇒ Object
Remove all “blank?” items for the array.
- #false? ⇒ Boolean
-
#first_index(value = nil, &block) ⇒ Object
return the first index of the array with a value matching that given.
-
#median ⇒ Object
Return the median digitized value of the array.
-
#numericize ⇒ Object
(also: #digitize)
Convert all items in the array to_f.
-
#swap_indexes(i, j) ⇒ Object
Return a copy of the array with values at the given indexes swapped.
-
#swap_indexes!(i, j) ⇒ Object
Swap the values of an array at the given indexes and return it.
- #to_boolean ⇒ Object (also: #to_b)
-
#true? ⇒ Boolean
[].true? => false [true].true? => true [true,false].true? => true [false].true? => false.
-
#true_xor_false? ⇒ Boolean
I need to work on this one …
Instance Method Details
#arrange(new_array_index = []) ⇒ Object
- ‘a’,‘b’,‘c’].arrange() => [‘c’,‘a’,‘b’
16 17 18 19 20 21 22 |
# File 'lib/ruby_extension/array.rb', line 16 def arrange(new_array_index=[]) new_array = self.dup new_array_index.each_with_index do |index,new_index| new_array[new_index] = self[index] end new_array end |
#average ⇒ Object
Return the average digitized value of the array
48 49 50 51 52 53 54 55 |
# File 'lib/ruby_extension/array.rb', line 48 def average if self.length > 0 # sum defined in activesupport/lib/active_support/core_ext/enumerable.rb self.digitize.sum.to_f / self.length else nil end end |
#capitalize ⇒ Object
Return capitlized versions of each item in the array
30 31 32 |
# File 'lib/ruby_extension/array.rb', line 30 def capitalize collect(&:capitalize) end |
#capitalize! ⇒ Object
Capitalize each item in the array and return it
35 36 37 38 39 40 |
# File 'lib/ruby_extension/array.rb', line 35 def capitalize! each_with_index do |element,index| self[index] = element.capitalize end self end |
#downcase ⇒ Object
Return downcased versions of each item in the array
43 44 45 |
# File 'lib/ruby_extension/array.rb', line 43 def downcase collect(&:downcase) end |
#drop_blanks! ⇒ Object
Remove all “blank?” items for the array
25 26 27 |
# File 'lib/ruby_extension/array.rb', line 25 def drop_blanks! delete_if{|a|a.blank?} end |
#false? ⇒ Boolean
123 124 125 |
# File 'lib/ruby_extension/array.rb', line 123 def false? !empty? && any?{|v| v.false? } end |
#first_index(value = nil, &block) ⇒ Object
return the first index of the array with a value matching that given
97 98 99 100 101 102 103 |
# File 'lib/ruby_extension/array.rb', line 97 def first_index(value = nil, &block) using_block = block_given? each_with_index do |element,index| return index if (using_block && yield(element)) || (value == element) end return nil end |
#median ⇒ Object
Return the median digitized value of the array
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ruby_extension/array.rb', line 58 def median if self.length > 0 sorted_values = self.digitize.sort length = sorted_values.length if length.odd? sorted_values[length/2] else ( sorted_values[length/2] + sorted_values[-1+length/2] ).to_f / 2 end else nil end end |
#numericize ⇒ Object Also known as: digitize
Convert all items in the array to_f
88 89 90 |
# File 'lib/ruby_extension/array.rb', line 88 def numericize collect(&:to_f) end |
#swap_indexes(i, j) ⇒ Object
Return a copy of the array with values at the given indexes swapped.
74 75 76 77 78 |
# File 'lib/ruby_extension/array.rb', line 74 def swap_indexes(i,j) new_array = self.dup new_array[i],new_array[j] = self[j],self[i] new_array end |
#swap_indexes!(i, j) ⇒ Object
Swap the values of an array at the given indexes and return it
82 83 84 85 |
# File 'lib/ruby_extension/array.rb', line 82 def swap_indexes!(i,j) self[i],self[j] = self[j],self[i] self end |
#to_boolean ⇒ Object Also known as: to_b
105 106 107 |
# File 'lib/ruby_extension/array.rb', line 105 def to_boolean !empty? && all?{|v| v.to_boolean } end |
#true? ⇒ Boolean
[].true?
> false
[true].true?
> true
[true,false].true?
> true
[false].true?
> false
119 120 121 |
# File 'lib/ruby_extension/array.rb', line 119 def true? !empty? && any?{|v| v.true? } end |
#true_xor_false? ⇒ Boolean
I need to work on this one …
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ruby_extension/array.rb', line 128 def true_xor_false? # self.include?('true') ^ self.include?('false') ^ # self.include?(true) ^ self.include?(false) contains_true = contains_false = false each {|v| # ( v.to_boolean ) ? contains_true = true : contains_false = true eval("contains_#{v.to_boolean}=true") } contains_true ^ contains_false end |