Re: Some ugly code (again)

fr Arnaud:

Can I do nicer than:

{1…y}.each do |y|

{1…x}.each do |x|

the_name = name + y.to_s + x.to_s

end

end

your code is already nice. it is readable to me.
“nicer” is difficult to measure :slight_smile:

if you do not like the “…” notation and
if you do not like the to_s conversion,

you can eg do something like,

cat test.rb

y=3
x=4

1.upto(y) do |i|
1.upto(x) do |j|
puts “rack#{i}#{j}”
end
end

or

y.times do |i|
x.times do |j|
puts “rack#{i+1}#{j+1}”
end
end

ruby test.rb
rack11
rack12
rack13
rack14
rack21
rack22
rack23
rack24
rack31
rack32
rack33
rack34

kind regards -botp

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs