arnaud
1
Hello!
I have (again) some ruby code to beautifulize
I have 2 Range {1…x} and {1…y} and a name
My goal is to have formatted names like : name + y + x
For example:
rack11 rack12 rack13
rack21 rack22 rack23
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
???
Arnaud.
arnaud
3
Hi,
I have 2 Range {1…x} and {1…y} and a name
the_name = name + y.to_s + x.to_s
end
end
???
If you want something one liner,
([‘rake’](xy)).zip((1…x).to_ay,(1…y).to_ax).each{|z|puts “#{z}”}
Or
((1…x).to_ay).zip((1…y).to_ax).each{|z|puts “rake#{z}”}
Regards,
Park H.
arnaud
4
arnaud stageman wrote:
Park H.
Beuh it’s remained me Perl
Thanks for your response Park.
and another one liner:
puts (0…x*y).map{|i| “#{name}#{i%x+1}#{i/x+1}”}
cheers
Simon
arnaud
5
Park H. wrote:
Hi,
I have 2 Range {1…x} and {1…y} and a name
the_name = name + y.to_s + x.to_s
end
end
???
If you want something one liner,
([‘rake’](xy)).zip((1…x).to_ay,(1…y).to_ax).each{|z|puts “#{z}”}
Or
((1…x).to_ay).zip((1…y).to_ax).each{|z|puts “rake#{z}”}
Regards,
Park H.
Beuh it’s remained me Perl
Thanks for your response Park.
arnaud
6
arnaud stageman wrote:
Arnaud.
The cartesian_product method doesn’t exist, you would have to write it.
It’s trivial
(1…y).cartesian_product((1…x)).map { |x| name + x.join("") }
arnaud
7
thank you all for your response but I cannot find what I want
Cheers
arnaud
8
arnaud stageman wrote:
{1…x}.each do |x|
the_name = name + y.to_s + x.to_s
end
end
Here’s a one-liner for ya:
(1…x).map{|x| (1…y).map{|y| “rack#{x}#{y}”}}.flatten
Cheers,
Daniel