Module: CommonLib::Array

Defined in:
lib/common_lib/ruby/array.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#arrange(new_array_index = []) ⇒ Object

a’,‘b’,‘c’].arrange() => [‘c’,‘a’,‘b’


4
5
6
7
8
9
10
# File 'lib/common_lib/ruby/array.rb', line 4

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

#averageObject

Return the average digitized value of the array



36
37
38
39
40
41
42
43
# File 'lib/common_lib/ruby/array.rb', line 36

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

#capitalizeObject

Return capitlized versions of each item in the array



18
19
20
# File 'lib/common_lib/ruby/array.rb', line 18

def capitalize
	collect(&:capitalize)
end

#capitalize!Object

Capitalize each item in the array and return it



23
24
25
26
27
28
# File 'lib/common_lib/ruby/array.rb', line 23

def capitalize!
	each_with_index do |element,index|
		self[index] = element.capitalize
	end
	self
end

#downcaseObject

Return downcased versions of each item in the array



31
32
33
# File 'lib/common_lib/ruby/array.rb', line 31

def downcase
	collect(&:downcase)
end

#drop_blanks!Object

Remove all “blank?” items for the array



13
14
15
# File 'lib/common_lib/ruby/array.rb', line 13

def drop_blanks!
	delete_if{|a|a.blank?}
end

#false?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/common_lib/ruby/array.rb', line 111

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



85
86
87
88
89
90
91
# File 'lib/common_lib/ruby/array.rb', line 85

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

#medianObject

Return the median digitized value of the array



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/common_lib/ruby/array.rb', line 46

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

#numericizeObject Also known as: digitize

Convert all items in the array to_f



76
77
78
# File 'lib/common_lib/ruby/array.rb', line 76

def numericize
	collect(&:to_f)
end

#swap_indexes(i, j) ⇒ Object

Return a copy of the array with values at the given indexes swapped.



62
63
64
65
66
# File 'lib/common_lib/ruby/array.rb', line 62

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



70
71
72
73
# File 'lib/common_lib/ruby/array.rb', line 70

def swap_indexes!(i,j)
	self[i],self[j] = self[j],self[i]
	self
end

#to_booleanObject Also known as: to_b



93
94
95
# File 'lib/common_lib/ruby/array.rb', line 93

def to_boolean
	!empty? && all?{|v| v.to_boolean }
end

#true?Boolean

[].true?

> false

[true].true?

> true

[true,false].true?

> true

[false].true?

> false

Returns:

  • (Boolean)


107
108
109
# File 'lib/common_lib/ruby/array.rb', line 107

def true?
	!empty? && any?{|v| v.true? }
end

#true_xor_false?Boolean

I need to work on this one …

Returns:

  • (Boolean)


116
117
118
119
120
121
122
123
124
125
# File 'lib/common_lib/ruby/array.rb', line 116

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