How to do pattern program in Ruby?

unknown wrote in post #1138260:

You don’t need an additional loop, just print the ‘\n’ after you have
finished printing the full line, i.e. after the loop.
I commented out the lines you want to get rid of.

n = 1
while n <= 5
1.upto(n) do |i|
#1.upto(n) do |i|
print i
end
print “\n”
n += 1
#end
end

Your answer helpful for me.

Thank you very much.

n = 1
while n <= 5
1.upto(n) do |i|
#1.upto(n) do |i|
print i
end
print “\n”
n += 1
#end
end

In above, I am getting correct output.

Now I want to get output like:

1
21
321
4321
54321

How can I get it?

Thank you.

Look up the Ruby Integer methods,

there is a fairly short list and you will be able to make a very small
change (change “1.upto(n)”) and get what you want.

“ruby-talk” [email protected] wrote on 02/28/2014
02:58:25
AM:

unknown wrote in post #1138332:

Look up the Ruby Integer methods,
Class: Integer (Ruby 2.1.1)
there is a fairly short list and you will be able to make a very small
change (change “1.upto(n)”) and get what you want.

“ruby-talk” [email protected] wrote on 02/28/2014
02:58:25
AM:

As you said, I use for loop in place of “1.upto(n)” then Is it possible
to get correct output?

Thank you.

unknown wrote in post #1138350:

No, don’t change to a for loop. the 1.upto is the preferred Ruby syntax.

You have
1.upto(n)
and get
1 2 … n

You want
n … 2 1

so, just reverse what the loop does,

n.downto(1)

Yes, I just not think in that way. At this my stage and my point of view
It’s very good answer.

Thank you very much.

No, don’t change to a for loop. the 1.upto is the preferred Ruby syntax.

You have
1.upto(n)
and get
1 2 … n

You want
n … 2 1

so, just reverse what the loop does,

n.downto(1)

“ruby-talk” [email protected] wrote on 02/28/2014
09:49:44
AM: