[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

loop questions

Lloyd Linklater

5/10/2007 12:33:00 PM

I am new to ruby and loving it so far and have beginner's questions
about loops.

I wanted something like this:

for (int i = 1; i <= 5; i++)
printf("%d\n", [i]);

expecting:

1
2
3
4
5


I wrote this:

i = 5

i.times do
puts i.to_s
end

And got:

5
5
5
5
5

I am guessing that there is an internal variable that is incremented.
doh! Is there a way to get the changing variable in there without
having a separate variable in there?

Anyway, I then tried this:

for i in 1..5
puts i.to_s
end

Which worked as expected. But I had better use for a descending loop.
In pascal it would be

for i := 5 downto 1 do
writeln('%d', [i]);

but I could not figure out how to do that in a for loop. I looked
through several books and could not find the answer. Any tips?

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

9 Answers

Robert Klemme

5/10/2007 12:46:00 PM

0

On 10.05.2007 14:33, Lloyd Linklater wrote:
> I am new to ruby and loving it so far and have beginner's questions
> about loops.
>
> I wanted something like this:
>
> for (int i = 1; i <= 5; i++)
> printf("%d\n", [i]);
>
> expecting:
>
> 1
> 2
> 3
> 4
> 5
>
>
> I wrote this:
>
> i = 5
>
> i.times do
> puts i.to_s
> end
>
> And got:
>
> 5
> 5
> 5
> 5
> 5
>
> I am guessing that there is an internal variable that is incremented.
> doh! Is there a way to get the changing variable in there without
> having a separate variable in there?

The current value us passed to the block. So rather do

5.times do |i|
puts i
end

> Anyway, I then tried this:
>
> for i in 1..5
> puts i.to_s
> end

You don't need the to_s here as puts will do that automatically.

> Which worked as expected. But I had better use for a descending loop.
> In pascal it would be
>
> for i := 5 downto 1 do
> writeln('%d', [i]);
>
> but I could not figure out how to do that in a for loop. I looked
> through several books and could not find the answer. Any tips?

5.downto 1 do |i|
puts i
end

or

5.step 1, -1 do |i|
puts i
end

If you think you need a /for/ loop:

for i in (1..5).to_a.reverse
puts i
end

But I'd rather not do this.

Kind regards

robert

Niko

5/10/2007 12:47:00 PM

0


Hi

5.times do |i|
puts i
end

0
1
2
3
4

1.upto(5) do |i|
puts i
end

1
2
3
4
5

5.downto(1) do |i|
puts i
end

5
4
3
2
1


Lloyd Linklater wrote:
> I am new to ruby and loving it so far and have beginner's questions
> about loops.
>
> I wanted something like this:
>
> for (int i = 1; i <= 5; i++)
> printf("%d\n", [i]);
>
> expecting:
>
> 1
> 2
> 3
> 4
> 5
>
>
> I wrote this:
>
> i = 5
>
> i.times do
> puts i.to_s
> end
>
> And got:
>
> 5
> 5
> 5
> 5
> 5
>
> I am guessing that there is an internal variable that is incremented.
> doh! Is there a way to get the changing variable in there without
> having a separate variable in there?
>
>
> Anyway, I then tried this:
>
> for i in 1..5
> puts i.to_s
> end
>
> Which worked as expected. But I had better use for a descending loop.
> In pascal it would be
>
> for i := 5 downto 1 do
> writeln('%d', [i]);
>
> but I could not figure out how to do that in a for loop. I looked
> through several books and could not find the answer. Any tips?
>
>


Harry Kakueki

5/10/2007 12:58:00 PM

0

On 5/10/07, Lloyd Linklater <lloyd@2live4.com> wrote:
>
> Which worked as expected. But I had better use for a descending loop.
> In pascal it would be
>
> for i := 5 downto 1 do
> writeln('%d', [i]);
>
> but I could not figure out how to do that in a for loop. I looked
> through several books and could not find the answer. Any tips?
>
> --
>
Is this what you want?

5.downto(1) do |x|
p x
end

OR

5.downto(1) {|x| p x}

Harry

--
http://www.kakueki.com/ruby...
A Look into Japanese Ruby List in English

Lloyd Linklater

5/10/2007 12:59:00 PM

0

Excellent! I *knew* that this language was top notch!

Thanks, gents! :)

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

Brian Candler

5/10/2007 1:01:00 PM

0

On Thu, May 10, 2007 at 09:33:08PM +0900, Lloyd Linklater wrote:
> Which worked as expected. But I had better use for a descending loop.
> In pascal it would be
>
> for i := 5 downto 1 do
> writeln('%d', [i]);
>
> but I could not figure out how to do that in a for loop. I looked
> through several books and could not find the answer. Any tips?

http://www.rubycentral.com/book/tut_std...

"Integers also support several useful iterators. We've seen one
already---7.times in the code example on page 47. Others include upto and
downto, for iterating up and down between two integers, and step, which is
more like a traditional for loop.

3.times { print "X " }
1.upto(5) { |i| print i, " " }
99.downto(95) { |i| print i, " " }
50.step(80, 5) { |i| print i, " " }

produces:

X X X 1 2 3 4 5 99 98 97 96 95 50 55 60 65 70 75 80"

(Or better, buy the second edition in paper or PDF form)

Regards,

Brian.

Lloyd Linklater

5/10/2007 1:32:00 PM

0

Excellent! I tried this right away and was able to make this mission
critical app:

b = " bottles of beer"
w = " on the wall. "
t = " Take one down and pass it around. "

99.downto(1) do |i|
puts i.to_s + b + w + i.to_s + b + "." + t + (i - 1).to_s + b + w
end

Thanks again!

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

John Joyce

5/10/2007 1:53:00 PM

0


On May 10, 2007, at 10:31 PM, Lloyd Linklater wrote:

> Excellent! I tried this right away and was able to make this mission
> critical app:
>
> b = " bottles of beer"
> w = " on the wall. "
> t = " Take one down and pass it around. "
>
> 99.downto(1) do |i|
> puts i.to_s + b + w + i.to_s + b + "." + t + (i - 1).to_s + b + w
> end
>
> Thanks again!
>
> --
> Posted via http://www.ruby-....
>

the | | is like a |shoot| or |dumb-waiter| every time through the
iteration or loop it recieves something sometimes you can pass it
more than one thing. At first it's a little weird but it really is
one of the most beautiful parts of ruby.

John Smith

5/10/2007 2:23:00 PM

0

http://www.surfjunky.com/?r... check this out :D

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

Brian Candler

5/10/2007 3:36:00 PM

0

On Thu, May 10, 2007 at 10:31:36PM +0900, Lloyd Linklater wrote:
> Excellent! I tried this right away and was able to make this mission
> critical app:
>
> b = " bottles of beer"
> w = " on the wall. "
> t = " Take one down and pass it around. "
>
> 99.downto(1) do |i|
> puts i.to_s + b + w + i.to_s + b + "." + t + (i - 1).to_s + b + w
> end

Whilst the following violates the Don't Repeat Yourself mantra, I think it's
even clearer like this:

99.downto(1) do |i|
puts <<EOS
#{i} bottles of beer on the wall.
#{i} bottles of beer.
Take one down and pass it around.
#{i-1} bottles of beer on the wall.
EOS
end

Actually, there's a bug which needs fixing:

def bottles(n)
"#{n} bottle#{n != 1 ? "s" : ""} of beer"
end
99.downto(1) { |i| puts <<EOS }
#{bottles(i)} on the wall.
#{bottles(i)}.
Take one down and pass it around.
#{bottles(i-1)} on the wall.
EOS