Avada Kedavra Avada Kedavra - 5 months ago 8
Ruby Question

How to split a string like the following example in ruby?

I want to split a string e.g.

"8737928"
into:

8.737928,
87.37928,
873.7928,
8737.928,
87379.28,
873792.8,
8.7.37928,
8.73.7928
...


Could someone help me?

Answer

This is probably not the best way, but this is how I'm doing it.

array = []
(0..63).each do |i| 
  i > 31 ? array[i] = "87" : array[i] = "8.7"
  i % 32 > 15 ? array[i] += "3" : array[i] += ".3"
  i % 16 > 7 ? array[i] += "7" : array[i] += ".7"
  i % 8 > 3 ? array[i] += "9" : array[i] += ".9"
  i % 4 > 1 ? array[i] += "2" : array[i] += ".2"
  i % 2 > 0 ? array[i] += "8" : array[i] += ".8"
end 
array.each{|i| puts i}

This comes from the idea that each spot a period can be is a boolean decision, and there are 6 possible spots. As 26 = 64, I looped from 0..63

Comments