Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/array.rb

Instance Method Summary collapse

Instance Method Details

#dimensionObject



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/array.rb', line 2

def dimension
	a = self
	return 0 if a.class != Array
	result = 1
	a.each do |sub_a|
		if sub_a.class == Array
			dim = sub_a.dimension
			result = dim + 1 if dim + 1 > result
		end
	end
	return result
end

#inject_dim(int) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/array.rb', line 25

def inject_dim(int)
	arr = self
	int.times do
		arr << []
	end
	arr
end

#insert_at(position_arr, value) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/array.rb', line 50

def insert_at(position_arr, value)
	arr = self
	if position_arr.size == 1
		arr[position_arr[0]] = value
		return(arr)
	else
		arr[position_arr[0]].insert_at(position_arr[1..-1], value)
	end
end

#matrix(int_arr, current_arr = []) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/array.rb', line 33

def matrix(int_arr, current_arr=[])		
	int = int_arr[0]
	new_int_arr = int_arr[1..-1]
	if int_arr.empty?
		return(current_arr)
	else
		if current_arr.empty?
			new_arr = current_arr.inject_dim(int)
			self.matrix(new_int_arr, new_arr)
		else
			current_arr.each do |arr|
				arr.matrix(int_arr, arr)
			end
		end
	end
end

#values_at_a(indices, current_array = self) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/array.rb', line 15

def values_at_a(indices, current_array=self)
	#in: self = [3,4,[6,5,[3,4]],3], indices = [2,2,0]
	#out: 3
	if indices.size == 1
		return(current_array[indices[0]])
	else
		values_at_a(indices[1..-1], current_array[indices[0]])
	end
end