Some ugly code (again)

Hello!

I have (again) some ruby code to beautifulize :slight_smile:

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 stageman wrote:

I have 2 Range {1…x} and {1…y} and a name

Oups!

replace {…} with (…)

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 stageman wrote:

Park H.

Beuh it’s remained me Perl :slight_smile:

Thanks for your response Park.

and another one liner:

puts (0…x*y).map{|i| “#{name}#{i%x+1}#{i/x+1}”}

cheers

Simon

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 :slight_smile:

Thanks for your response Park.

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("") }

thank you all for your response but I cannot find what I want

Cheers

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

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