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.

路路路

--
Posted via http://www.ruby-forum.com/.

arnaud stageman wrote:

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

Oups!

replace {..} with (..)

路路路

--
Posted via http://www.ruby-forum.com/\.

Hi,

From: arnaud stageman <mpepito13@gmail.com>
Reply-To: ruby-talk@ruby-lang.org
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: Some ugly code (again)
Date: Mon, 26 Jun 2006 17:40:09 +0900

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

???

If you want something one liner,

(['rake']*(x*y)).zip((1..x).to_a*y,(1..y).to_a*x).each{|z|puts "#{z}"}

Or

((1..x).to_a*y).zip((1..y).to_a*x).each{|z|puts "rake#{z}"}

Regards,

Park Heesob

arnaud stageman wrote:

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

Here's a one-liner for ya:

   (1..x).map{|x| (1..y).map{|y| "rack#{x}#{y}"}}.flatten

Cheers,
Daniel

arnaud stageman wrote:

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

???

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

Park Heesob 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']*(x*y)).zip((1..x).to_a*y,(1..y).to_a*x).each{|z|puts "#{z}"}

Or

((1..x).to_a*y).zip((1..y).to_a*x).each{|z|puts "rake#{z}"}

Regards,

Park Heesob

Beuh it's remained me Perl :slight_smile:

Thanks for your response Park.

路路路

--
Posted via http://www.ruby-forum.com/\.

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

Cheers

路路路

--
Posted via http://www.ruby-forum.com/.

arnaud stageman wrote:

If you want something one liner,

(['rake']*(x*y)).zip((1..x).to_a*y,(1..y).to_a*x).each{|z|puts "#{z}"}

Or

((1..x).to_a*y).zip((1..y).to_a*x).each{|z|puts "rake#{z}"}

Regards,

Park Heesob

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