[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Different Ways To Loop

Wyatt Greene

5/4/2008 2:07:00 AM

I love the flexibility of Ruby. It gives you several ways to do
something, leaving you to pick the most appropriate one. In the
spirit of showing off the flexibility of Ruby, I'd like to see how
many different ways there are to write a loop that prints the numbers
1 to 100, one on each line. I'll start off:

1.upto(100) { |x| puts x }

I know there's at least a dozen more ways to do this...

18 Answers

Bill Kelly

5/4/2008 2:20:00 AM

0


From: "Wyatt Greene" <greenewm@yahoo.com>
>
> 1.upto(100) { |x| puts x }

Ruby can even do the looping on our behalf. :)

puts (1..100).to_a



Regards,

Bill



Linux Devil

5/4/2008 2:24:00 AM

0

Wyatt Greene wrote:
> I love the flexibility of Ruby. It gives you several ways to do
> something, leaving you to pick the most appropriate one. In the
> spirit of showing off the flexibility of Ruby, I'd like to see how
> many different ways there are to write a loop that prints the numbers
> 1 to 100, one on each line. I'll start off:
>
> 1.upto(100) { |x| puts x }
>
> I know there's at least a dozen more ways to do this...

100.times {|x| puts x + 1}
(1..100).to_a.each {|x| puts x}
--
Posted via http://www.ruby-....

Wyatt Greene

5/4/2008 2:26:00 AM

0

On May 3, 10:24 pm, Linux Devil <devil.of.li...@gmail.com> wrote:
> Wyatt Greene wrote:
> > I love the flexibility of Ruby. It gives you several ways to do
> > something, leaving you to pick the most appropriate one. In the
> > spirit of showing off the flexibility of Ruby, I'd like to see how
> > many different ways there are to write a loop that prints the numbers
> > 1 to 100, one on each line. I'll start off:
>
> > 1.upto(100) { |x| puts x }
>
> > I know there's at least a dozen more ways to do this...
>
> 100.times {|x| puts x + 1}
> (1..100).to_a.each {|x| puts x}
> --
> Posted viahttp://www.ruby-....

p *1..100

ruby-talk

5/4/2008 2:28:00 AM

0

Quoting Linux Devil <devil.of.linux@gmail.com>:

>
> 100.times {|x| puts x + 1}
> (1..100).to_a.each {|x| puts x}
> --
> Posted via http://www.ruby-....
>
>

You don't even need the to_a part

(1..100).each { |x| p x }


Wyatt Greene

5/4/2008 2:31:00 AM

0

On May 3, 10:27 pm, ruby-t...@waltco.biz wrote:
> Quoting Linux Devil <devil.of.li...@gmail.com>:
>
>
>
> > 100.times {|x| puts x + 1}
> > (1..100).to_a.each {|x| puts x}
> > --
> > Posted viahttp://www.ruby-....
>
> You don't even need the to_a part
>
> (1..100).each { |x| p x }

count = 1; loop { puts count; count += 1; break if count > 100; }

ara.t.howard

5/4/2008 3:07:00 AM

0


On May 3, 2008, at 8:10 PM, Wyatt Greene wrote:
> 1.upto(100) { |x| puts x }


here are a few

# 1
puts Array.new(100){|i| i + 1}

# 2
i = 0 and loop do
puts i+=1
break if i >= 100
end

# 3
puts <<-chars.strip.split(%r//).map{|c| c[0]}
\001\002\003\004\005\006\a\b\t\n\v\f\r
\016\017\020\021\022\023\024\025\026\027\030\031\032\e\034\035\036\037!
\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcd
chars

# 4
100.times{|i| puts i + 1}



a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Dan Zwell

5/4/2008 3:49:00 AM

0

Don't forget the "traditional" style loop:

for x in 1..100 do
puts x+1
end

ara.t.howard wrote:
>
> On May 3, 2008, at 8:10 PM, Wyatt Greene wrote:
>> 1.upto(100) { |x| puts x }
>
>
> here are a few
>
> # 1
> puts Array.new(100){|i| i + 1}
>
> # 2
> i = 0 and loop do
> puts i+=1
> break if i >= 100
> end
>
> # 3
> puts <<-chars.strip.split(%r//).map{|c| c[0]}
> \001\002\003\004\005\006\a\b\t\n\v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\e\034\035\036\037!\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcd
>
> chars
>
> # 4
> 100.times{|i| puts i + 1}
>
>
>
> a @ http://codeforp...
> --
> we can deny everything, except that we have the possibility of being
> better. simply reflect on that.
> h.h. the 14th dalai lama
>
>
>
>
>


Mike McKinney

5/4/2008 5:55:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

of course there's...

i=0 and while(i<100) do
puts i+=1
end

i=0 and until(i==100) do
puts i+=1
end




On Sat, May 3, 2008 at 11:48 PM, Dan Zwell <dzwell@gmail.com> wrote:

> Don't forget the "traditional" style loop:
>
> for x in 1..100 do
> puts x+1
> end
>
>
> ara.t.howard wrote:
>
> >
> > On May 3, 2008, at 8:10 PM, Wyatt Greene wrote:
> >
> > > 1.upto(100) { |x| puts x }
> > >
> >
> >
> > here are a few
> >
> > # 1
> > puts Array.new(100){|i| i + 1}
> >
> > # 2
> > i = 0 and loop do
> > puts i+=1
> > break if i >= 100
> > end
> >
> > # 3
> > puts <<-chars.strip.split(%r//).map{|c| c[0]}
> > \001\002\003\004\005\006\a\b\t\n\v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\e\034\035\036\037!\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcd
> >
> > chars
> >
> > # 4
> > 100.times{|i| puts i + 1}
> >
> >
> >
> > a @ http://codeforp...
> > --
> > we can deny everything, except that we have the possibility of being
> > better. simply reflect on that.
> > h.h. the 14th dalai lama
> >
> >
> >
> >
> >
> >
>
>


--
Aloha!

Mike McKinney
mike@huikau.com
(http://blog....)

Wyatt Greene

5/4/2008 12:53:00 PM

0

On May 4, 1:54 am, Mike McKinney <r...@huikau.com> wrote:
> [Note: parts of this message were removed to make it a legal post.]
>
> of course there's...
>
> i=0 and while(i<100) do
> puts i+=1
> end
>
> i=0 and until(i==100) do
> puts i+=1
> end
>
>
>
> On Sat, May 3, 2008 at 11:48 PM, Dan Zwell <dzw...@gmail.com> wrote:
> > Don't forget the "traditional" style loop:
>
> > for x in 1..100 do
> > puts x+1
> > end
>
> > ara.t.howard wrote:
>
> > > On May 3, 2008, at 8:10 PM, Wyatt Greene wrote:
>
> > > > 1.upto(100) { |x| puts x }
>
> > > here are a few
>
> > > # 1
> > > puts Array.new(100){|i| i + 1}
>
> > > # 2
> > > i = 0 and loop do
> > > puts i+=1
> > > break if i >= 100
> > > end
>
> > > # 3
> > > puts <<-chars.strip.split(%r//).map{|c| c[0]}
> > > \001\002\003\004\005\006\a\b\t\n\v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\e\034\035\036\037!\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcd
>
> > > chars
>
> > > # 4
> > > 100.times{|i| puts i + 1}
>
> > > a @http://codeforp...
> > > --
> > > we can deny everything, except that we have the possibility of being
> > > better. simply reflect on that.
> > > h.h. the 14th dalai lama
>
> --
> Aloha!
>
> Mike McKinney
> m...@huikau.com
> (http://blog....)

And here's an "object-oriented" way:

class Counter
def count
@count ||= 1
puts @count
@count += 1
@count > 100
end
end

counter = Counter.new
until counter.count; end

Ashley Wharton

5/4/2008 1:15:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Sun, May 4, 2008 at 8:55 AM, Wyatt Greene <greenewm@yahoo.com> wrote:

> On May 4, 1:54 am, Mike McKinney <r...@huikau.com> wrote:
> > [Note: parts of this message were removed to make it a legal post.]
> >
> > of course there's...
> >
> > i=0 and while(i<100) do
> > puts i+=1
> > end
> >
> > i=0 and until(i==100) do
> > puts i+=1
> > end
> >
> >
> >
> > On Sat, May 3, 2008 at 11:48 PM, Dan Zwell <dzw...@gmail.com> wrote:
> > > Don't forget the "traditional" style loop:
> >
> > > for x in 1..100 do
> > > puts x+1
> > > end
> >
> > > ara.t.howard wrote:
> >
> > > > On May 3, 2008, at 8:10 PM, Wyatt Greene wrote:
> >
> > > > > 1.upto(100) { |x| puts x }
> >
> > > > here are a few
> >
> > > > # 1
> > > > puts Array.new(100){|i| i + 1}
> >
> > > > # 2
> > > > i = 0 and loop do
> > > > puts i+=1
> > > > break if i >= 100
> > > > end
> >
> > > > # 3
> > > > puts <<-chars.strip.split(%r//).map{|c| c[0]}
> > > >
> \001\002\003\004\005\006\a\b\t\n\v\f\r\016\017\020\021\022\023\024\025\026\027\030\031\032\e\034\035\036\037!\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcd
> >
> > > > chars
> >
> > > > # 4
> > > > 100.times{|i| puts i + 1}
> >
> > > > a @http://codeforp...
> > > > --
> > > > we can deny everything, except that we have the possibility of being
> > > > better. simply reflect on that.
> > > > h.h. the 14th dalai lama
> >
> > --
> > Aloha!
> >
> > Mike McKinney
> > m...@huikau.com
> > (http://blog....)
>
> And here's an "object-oriented" way:
>
> class Counter
> def count
> @count ||= 1
> puts @count
> @count += 1
> @count > 100
> end
> end
>
> counter = Counter.new
> until counter.count; end
>
> Based on a response from Eric when I first asked for alternative ways to
do this:

MAX = 100
def print_from_to(start, stop)
start.upto(stop) do |number|
puts number
end
end
loop do
number = 1
print_from_to(number, MAX)
break
end