How to do pattern program in Ruby?

Correct, you only want a newline at the end of each line.
You will want a 'puts' outside of the inner loop.

n = 1
while n <= 5
  1.upto(n) do |i|
    print "#{i} "
  end
  puts
  n += 1
end

From: Jaimin Pandya <lists@ruby-forum.com>
To: ruby-talk@ruby-lang.org
Date: 02/27/2014 07:55 AM
Subject: Re: How to do pattern program in Ruby?
Sent by: "ruby-talk" <ruby-talk-bounces@ruby-lang.org>

Panagiotis Atmatzidis wrote in post #1138192:
> Hey again,
>
> Sorry for the previous mail, was out of line, you have the code

there...

···

"ruby-talk" <ruby-talk-bounces@ruby-lang.org> wrote on 02/27/2014 07:55:01 AM:

> and I'm terribly sleepy.

>>
>> I am trying to do as follow:
>>
>> n = 1
>> while n <= 5
>> 1.upto(n) do |i|
>> print "#{i} "
>
> Just add the "\n" (newline) and you're all set: print "#{i}\n"
>
By adding "\n" , i am not able to get desire output.

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

Jaimin Pandya wrote in post #1138235:

Panagiotis Atmatzidis wrote in post #1138192:

Hey again,

Sorry for the previous mail, was out of line, you have the code there...
and I'm terribly sleepy.

I am trying to do as follow:

n = 1
while n <= 5
1.upto(n) do |i|
      print "#{i} "

Just add the "\n" (newline) and you're all set: print "#{i}\n"

By adding "\n" , i am not able to get desire output.

Sorry for reply to early and I said by adding "\n" , I am not getting
desire output.

By adding "\n" , I am able to get desire output by using nested loop.

Thank you.

···

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

Jaimin Pandya wrote in post #1137859:

Jaimin Pandya wrote in post #1137849:

can I do that?

Yes you (should) can...

n = 1
while n < 6
   puts "6 5 4 3 2 1"[2*(6-n)..12]
   n += 1
end

···

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

I would not have thought of using string indexing like that.
I prefer not having the hard coded maximum string.
How about this, the max is variable and it centers the rows.

max = 6
1.upto(max) do |i|
  line = ""
      i.downto(1) {|j| line += "#{j} "}
  puts line.rjust(max + i)
end

···

"ruby-talk" <ruby-talk-bounces@ruby-lang.org> wrote on 02/24/2014 02:11:33 PM:

From: Regis d'Aubarede <lists@ruby-forum.com>
To: ruby-talk@ruby-lang.org
Date: 02/24/2014 02:12 PM
Subject: Re: How to do pattern program in Ruby?
Sent by: "ruby-talk" <ruby-talk-bounces@ruby-lang.org>

Jaimin Pandya wrote in post #1137859:
>> Jaimin Pandya wrote in post #1137849:

> can I do that?
Yes you (should) can...

n = 1
while n < 6
   puts "6 5 4 3 2 1"[2*(6-n)..12]
   n += 1
end

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

unknown wrote in post #1137867:

I would not have thought of using string indexing like that.

No list, no string slicing, ... , why use ruby ?
C is enough for that !

:slight_smile:

···

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