- Previous thread: [ANN] pik 0.2.1 Released
- Next thread: BDB::Fatal error from WordNet
- Threads sorted by date: ruby 200910
if there is a loop
35.times do |i|
print i
# do something here...
end
it is nice except i hope to count down the "i"...
it can be
print 35 - i
but then if the number needs to be 36 then i need to change both places.
or it can be
35.downto(1) do |i|
but doing so feel like needing to do some counting... 1 to 35 is in fact
35 times... so that's good
if 35.times can go decrement that would be nice too.
thanks.
--
Posted via http://www.ruby-forum.com/.
35.times do |i|
print i
# do something here...
end
it is nice except i hope to count down the "i"...
it can be
print 35 - i
but then if the number needs to be 36 then i need to change both places.
or it can be
35.downto(1) do |i|
but doing so feel like needing to do some counting... 1 to 35 is in fact
35 times... so that's good
if 35.times can go decrement that would be nice too.
thanks.
--
Posted via http://www.ruby-forum.com/.
Jian Lin wrote:
> if there is a loop
>
> 35.times do |i|
> print i
> # do something here...
> end
>
> it is nice except i hope to count down the "i"...
>
> it can be
>
> print 35 - i
>
> but then if the number needs to be 36 then i need to change both places.
>
> or it can be
>
> 35.downto(1) do |i|
>
> but doing so feel like needing to do some counting... 1 to 35 is in fact
> 35 times... so that's good
>
> if 35.times can go decrement that would be nice too.
>
> thanks.
Not sure I follow what you're asking, or why downto is not a good solution? but
you could try this:
irb(main):008:0> count=5
irb(main):009:0> count.times { |n| puts count-n }
5
4
3
2
1
=> 5
--
Kind Regards,
Rajinder Yadav
http://DevMentor.org
Do Good ~ Share Freely
> if there is a loop
>
> 35.times do |i|
> print i
> # do something here...
> end
>
> it is nice except i hope to count down the "i"...
>
> it can be
>
> print 35 - i
>
> but then if the number needs to be 36 then i need to change both places.
>
> or it can be
>
> 35.downto(1) do |i|
>
> but doing so feel like needing to do some counting... 1 to 35 is in fact
> 35 times... so that's good
>
> if 35.times can go decrement that would be nice too.
>
> thanks.
Not sure I follow what you're asking, or why downto is not a good solution? but
you could try this:
irb(main):008:0> count=5
irb(main):009:0> count.times { |n| puts count-n }
5
4
3
2
1
=> 5
--
Kind Regards,
Rajinder Yadav
http://DevMentor.org
Do Good ~ Share Freely
Rajinder Yadav wrote:
> Jian Lin wrote:
>>
>>
>> if 35.times can go decrement that would be nice too.
>>
>> thanks.
>
> Not sure I follow what you're asking, or why downto is not a good
> solution? but
> you could try this:
>
> irb(main):008:0> count=5
> irb(main):009:0> count.times { |n| puts count-n }
1. I don't want to create an extra variable. It is a simple loop to
count down 35 times in a short program.
2. I want 35.times because it says it is 35 times, very clearly.
35.downto(1) you will need to think a little how many times it is. My
purpose is to do it 35 times, so 35.times is best, but I need the count
down. Something like
35.times.countdown do |i|
print i, " "
# do something
end
--
Posted via http://www.ruby-forum.com/.
> Jian Lin wrote:
>>
>>
>> if 35.times can go decrement that would be nice too.
>>
>> thanks.
>
> Not sure I follow what you're asking, or why downto is not a good
> solution? but
> you could try this:
>
> irb(main):008:0> count=5
> irb(main):009:0> count.times { |n| puts count-n }
1. I don't want to create an extra variable. It is a simple loop to
count down 35 times in a short program.
2. I want 35.times because it says it is 35 times, very clearly.
35.downto(1) you will need to think a little how many times it is. My
purpose is to do it 35 times, so 35.times is best, but I need the count
down. Something like
35.times.countdown do |i|
print i, " "
# do something
end
--
Posted via http://www.ruby-forum.com/.
On Oct 20, 2009, at 12:44 AM, Jian Lin wrote:
> Rajinder Yadav wrote:
>> Jian Lin wrote:
>>>
>>>
>>> if 35.times can go decrement that would be nice too.
>>>
>>> thanks.
>>
>> Not sure I follow what you're asking, or why downto is not a good
>> solution? but
>> you could try this:
>>
>> irb(main):008:0> count=5
>> irb(main):009:0> count.times { |n| puts count-n }
>
> 1. I don't want to create an extra variable. It is a simple loop to
> count down 35 times in a short program.
>
> 2. I want 35.times because it says it is 35 times, very clearly.
> 35.downto(1) you will need to think a little how many times it is.
> My
> purpose is to do it 35 times, so 35.times is best, but I need the
> count
> down. Something like
>
> 35.times.countdown do |i|
> print i, " "
> # do something
> end
>
> --
> Posted via http://www.ruby-forum.com/.
>
So DO IT that way. Ruby lets you!
irb> class Integer
def countdown
self.downto(1){|i|yield i}
end
end
=> nil
irb> 35.countdown {|i| print i, ' '}; puts "Boom!"
35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13
12 11 10 9 8 7 6 5 4 3 2 1 Boom!
=> nil
Happy?
-Rob
Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
Rob Biedenharn wrote:
> irb> class Integer
> def countdown
> self.downto(1){|i|yield i}
> end
> end
> => nil
> irb> 35.countdown {|i| print i, ' '}; puts "Boom!"
> 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13
> 12 11 10 9 8 7 6 5 4 3 2 1 Boom!
yup, that's similar to what i was looking for. And 35.times doesn't
have any mechanism to count down i guess, not like
for i = 35 to 1 step -1
do something
next
--
Posted via http://www.ruby-forum.com/.
> irb> class Integer
> def countdown
> self.downto(1){|i|yield i}
> end
> end
> => nil
> irb> 35.countdown {|i| print i, ' '}; puts "Boom!"
> 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13
> 12 11 10 9 8 7 6 5 4 3 2 1 Boom!
yup, that's similar to what i was looking for. And 35.times doesn't
have any mechanism to count down i guess, not like
for i = 35 to 1 step -1
do something
next
--
Posted via http://www.ruby-forum.com/.
On Tue, Oct 20, 2009 at 1:14 PM, Jian Lin wrote:
> for i =3D 35 to 1 step -1
hmm if you modify the step var, the block would not loop 35 times, wc
you ask in your op
> =A0do something
> next
yours was a special case, so i was thinking something like,
> class Fixnum
> def countdown_by decrement =3D 1
> x =3D self
> x.times do |y|
> yield(x - decrement*y)
> end
> end
> end
>
> 5.countdown_by {|x| puts x}
5
4
3
2
1
=3D> 5
> 5.countdown_by(2) {|x| puts x}
5
3
1
-1
-3
=3D> 5
kind regards -botp
> for i =3D 35 to 1 step -1
hmm if you modify the step var, the block would not loop 35 times, wc
you ask in your op
> =A0do something
> next
yours was a special case, so i was thinking something like,
> class Fixnum
> def countdown_by decrement =3D 1
> x =3D self
> x.times do |y|
> yield(x - decrement*y)
> end
> end
> end
>
> 5.countdown_by {|x| puts x}
5
4
3
2
1
=3D> 5
> 5.countdown_by(2) {|x| puts x}
5
3
1
-1
-3
=3D> 5
kind regards -botp
On Oct 20, 2009, at 1:14 AM, Jian Lin wrote:
> Rob Biedenharn wrote:
>
>> irb> class Integer
>> def countdown
>> self.downto(1){|i|yield i}
>> end
>> end
>> => nil
>> irb> 35.countdown {|i| print i, ' '}; puts "Boom!"
>> 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13
>> 12 11 10 9 8 7 6 5 4 3 2 1 Boom!
>
> yup, that's similar to what i was looking for. And 35.times doesn't
> have any mechanism to count down i guess, not like
>
> for i = 35 to 1 step -1
> do something
> next
>
> --
> Posted via http://www.ruby-forum.com/.
>
Just use Integer#step if that's how you want to think about it:
irb> 35.step(1,-1) {|i| print i,' '}; puts "Ha!"
35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13
12 11 10 9 8 7 6 5 4 3 2 1 Ha!
=> nil
-Rob
Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
#Ruby has "reverse_each" method.
irb(main):001:0> RUBY_VERSION
=> "1.9.1"
irb(main):002:0> 35.times {|i| print i, " "}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29
30 31 32 33 34 => 35
irb(main):003:0> 35.times.reverse_each {|i| print i, " "}
34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11
10 9 8 7
6 5 4 3 2 1 0 => #
#Note that it counts 34 down to 0 (not 35 down to 1).
--
Posted via http://www.ruby-forum.com/.
irb(main):001:0> RUBY_VERSION
=> "1.9.1"
irb(main):002:0> 35.times {|i| print i, " "}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
27 28 29
30 31 32 33 34 => 35
irb(main):003:0> 35.times.reverse_each {|i| print i, " "}
34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11
10 9 8 7
6 5 4 3 2 1 0 => #
#Note that it counts 34 down to 0 (not 35 down to 1).
--
Posted via http://www.ruby-forum.com/.
Thairuby TH wrote:
> irb(main):003:0> 35.times.reverse_each {|i| print i, " "}
> 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11
> 10 9 8 7
> 6 5 4 3 2 1 0 => #
>
> #Note that it counts 34 down to 0 (not 35 down to 1).
counting from 34 to 0 is better since it is good for rocket take off =)
--
Posted via http://www.ruby-forum.com/.
> irb(main):003:0> 35.times.reverse_each {|i| print i, " "}
> 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11
> 10 9 8 7
> 6 5 4 3 2 1 0 => #
>
> #Note that it counts 34 down to 0 (not 35 down to 1).
counting from 34 to 0 is better since it is good for rocket take off =)
--
Posted via http://www.ruby-forum.com/.
Related Threads
- Hendrix - Make a feedback directly on the help browser's bar - firefox
- 2nd Workshop on Controlled Natural Languages CNL 2010: Call for Participation - web
- status=bounced unknown user: - postfix
- is not operator? - python
- svn commit: r963009 - in /camel/trunk: camel-core/src/test/java/org/apache/camel/management/JmxInstrumentationUsingDefaultsTest. - apachecamel
- Reloading the full schema in upgradeprovision - samba
- Hendrix - Problema con Firefox 3.6.6 Flash no funciona en servidor web - firefox
- PgWest 2010 Call for Papers! - django
- unsubscribe - ruby
- Wine - No Text in Sims 3 - wine
- jquery-ui - themeswitchertool.js cookie fix - jquery
- kde-linux - KDevelop - kde