Class: String

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

Instance Method Summary collapse

Instance Method Details

#current_level_information(b) ⇒ Object

in: “[1,2,,[4,2,[3,2,]],2,]” out: [1,2,,[4,2,[3,2,]],2,]



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/string.rb', line 22

def current_level_information(b)
	b = b.gsub(" ","")
	stripped_array = b[1..-2]
	in_array = 0
	inside_arrays_string = ""
	inside_values_string = ""
	stripped_array.split("").each do |char|
		if char == "["
			in_array += 1
		elsif char == "]"
			in_array += -1
		end
		if (in_array > 0) || (char == "]")
			inside_arrays_string += char
		end
	end
	stripped_array_without_arrays = stripped_array
	inside_arrays_string.gsub("][","],,,[").split(",,,").each do |str|
		stripped_array_without_arrays = stripped_array_without_arrays.gsub(str,"")
	end
	inside_values_string = stripped_array_without_arrays.split(",").find_all{|e|e!=""}.join(",")
	return {:values => inside_values_string, :arrays => inside_arrays_string}
end

#index_array(str) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/string.rb', line 67

def index_array(str)
	indices = []
	string = self
	ignore_indices = []
	search_length = str.size
	[*(0..string.size-1)].each do |i|
		if !ignore_indices.include?(i)
			compare_str = string[i..(i+search_length-1)]
			if compare_str == str
				indices << i
				ignore_indices = ignore_indices + [i..(i+search_length-1)]
			end
		end
	end
	return(indices)
end

#paren_to_arrayObject



2
3
4
5
6
7
8
# File 'lib/string.rb', line 2

def paren_to_array
	#in: "(2..5)"
	#out: "[2,3,4,5]"
	start = self[1].to_i
	stop = self[-2].to_i
	(start..stop).map{|i|i}.to_s
end

#sub_paren_with_arrayObject



10
11
12
13
14
15
16
17
# File 'lib/string.rb', line 10

def sub_paren_with_array
	text = self
	targets = text.scan(/\([\d]+\.\.[\d]+\)/)
	targets.each do |target|
		text = text.gsub(target, target.paren_to_array)
	end
	return(text)
end

#to_aObject



63
64
65
# File 'lib/string.rb', line 63

def to_a
	self.to_array
end

#to_array(current_array = [self]) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/string.rb', line 19

def to_array(current_array=[self])
	#in: "[1,2,[3,4],[4,2,[3,2,[4,2]]],2,[4,2]]"
	#out: [1,2,[3,4],[4,2,[3,2,[4,2]]],2,[4,2]]
	def current_level_information(b)
		b = b.gsub(" ","")
		stripped_array = b[1..-2]
		in_array = 0
		inside_arrays_string = ""
		inside_values_string = ""
		stripped_array.split("").each do |char|
			if char == "["
				in_array += 1
			elsif char == "]"
				in_array += -1
			end
			if (in_array > 0) || (char == "]")
				inside_arrays_string += char
			end
		end
		stripped_array_without_arrays = stripped_array
		inside_arrays_string.gsub("][","],,,[").split(",,,").each do |str|
			stripped_array_without_arrays = stripped_array_without_arrays.gsub(str,"")
		end
		inside_values_string = stripped_array_without_arrays.split(",").find_all{|e|e!=""}.join(",")
		return {:values => inside_values_string, :arrays => inside_arrays_string}
	end
	if !current_array.join(",").include?("[")
		return(current_array)
	else
		a = []
		element = current_array.find_all{|e|e.include?("[")}.first
		i = current_array.index(element)
		info = current_level_information(element)
		info[:values].split(",").each do |v|
			a << v
		end
		info[:arrays].gsub("][","],,,[").split(",,,").each do |v|
			a << v.to_array
		end
		current_array[i] = a
		return(current_array[0])
	end
end