Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/opl.rb
Instance Method Summary collapse
-
#current_level_information(b) ⇒ Object
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]].
- #paren_to_array ⇒ Object
- #sub_paren_with_array ⇒ Object
- #to_a ⇒ Object
- #to_array(current_array = [self]) ⇒ Object
Instance Method Details
#current_level_information(b) ⇒ Object
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]]
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/opl.rb', line 71 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 |
#paren_to_array ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/opl.rb', line 50 def paren_to_array #in: "(2..5)" #out: "[2,3,4,5]" text = self start = text[1].to_i stop = text[-2].to_i (start..stop).map{|i|i}.to_s end |
#sub_paren_with_array ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/opl.rb', line 59 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_a ⇒ Object
112 113 114 |
# File 'lib/opl.rb', line 112 def to_a self.to_array end |
#to_array(current_array = [self]) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/opl.rb', line 68 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 |