// generate code for parsing an array of array
tab = [ ["a","b"] , ["c","e"] , ["d"]]
def generation(tab)
metastring = ""
length = tab.length
i = 0
indent = " "
for content in tab
if i != 0
metastring += "for t#{i} in tab"
j = i
while j > 0
metastring += "-[t#{(j-1)}]"
j = j - 1
end
metastring += "\n"
metastring += indent*(i+1)
i = i +1
elsif
metastring += "for t#{i} in tab\n"
metastring += indent
i = i +1
end
end
temp = i
i = 0
for content in tab
if i != 0
metastring += "for u#{i} in t#{i}"
j = i
while j > 0
metastring += "-[u#{(j-1)}]"
j = j - 1
end
metastring += "\n"
metastring += indent*(temp+i+1)
i = i +1
elsif
metastring += "for u#{i} in t#{i}\n"
metastring += indent*(temp+1)
i = i +1
end
end
metastring += "\n"
metastring += (indent*tab.length*2)+"puts\" "
i = 0
for content in tab
metastring += "\#{u#{i}} "
i += 1
end
metastring += "\"\n"
metastring += "\n"
i = 0
while i < tab.length*2
i += 1
metastring += (indent*((tab.length*2)-i))+"end\n"
end
return metastring
end
puts generation(tab)
Refactorings
No refactoring yet !
Adam
August 11, 2010, August 11, 2010 16:23, permalink
Not sure why you are generating code here over just executing, but to keep with that spirit...
tab = [ ["a","b"] , ["c","e"] , ["d"]]
def generate(tab)
<<-EOS
tab.flatten.permutation(3).each do |permutation|
puts permutation.join(' ')
end
EOS
end
It's dirty but it's work...