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,]



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

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



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

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
# File 'lib/string.rb', line 2

def paren_to_array
  #in: "(2..5)"
  #out: "[2,3,4,5]"
  eval(self).map{|i|i}.to_s
end

#sub_paren_with_arrayObject



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

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



61
62
63
# File 'lib/string.rb', line 61

def to_a
  self.to_array
end

#to_array(current_array = [self]) ⇒ Object



17
18
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
# File 'lib/string.rb', line 17

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