Different Ways To Loop

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...

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

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

puts (1..100).to_a

Regards,

Bill

···

From: "Wyatt Greene" <greenewm@yahoo.com>

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-forum.com/\.

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://codeforpeople.com/

···

On May 3, 2008, at 8:10 PM, Wyatt Greene wrote:

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

--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

([nil]*100).each_with_index{|_,i| puts i+1}
Array.new(100).each_with_index{|_,i| puts i+1}

···

On Sat, 03 May 2008 19:07:12 -0700, 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...

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

I haven't seen 'retry' so far:

10:48:13 RTEConnector$ ruby -e 'i=0;begin;puts i;raise
"X";rescue;i+=1;retry if i<=10;end'
0
1
2
3
4
5
6
7
8
9
10
10:48:25 RTEConnector$

Cheers

robert

···

2008/5/4 Wyatt Greene <greenewm@yahoo.com>:

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...

--
use.inject do |as, often| as.you_can - without end

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-forum.com/\.

You don't even need the to_a part

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

p *1..100

···

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-forum.com/.

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://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being better. simply reflect on that.
h.h. the 14th dalai lama

Nice ones, Ken! :slight_smile:

Here's another one, lifted from page 128 of "The Ruby Programming
Language":

x = 10
puts x = x + 1 while x < 100

···

On May 4, 3:22 pm, Ken Bloom <kbl...@gmail.com> wrote:

On Sat, 03 May 2008 19:07:12 -0700, 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...

([nil]*100).each_with_index{|_,i| puts i+1}
Array.new(100).each_with_index{|_,i| puts i+1}

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.http://www.iit.edu/~kbloom1/

Hmm, the example with p *1..100 surprised me, I knew the * but I somehow
havent seen this used in such a short way :slight_smile:

I guess fun examples would be that noone notices that a loop is a
work... isn't there an obfuscated way with Array(pack/unpack)?

···

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

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

···

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-forum.com/.

You don't even need the to_a part

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

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://codeforpeople.com/
> --
> 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.huikau.com)

Hi --

Hmm, the example with p *1..100 surprised me, I knew the * but I somehow
havent seen this used in such a short way :slight_smile:

I guess fun examples would be that noone notices that a loop is a
work... isn't there an obfuscated way with Array(pack/unpack)?

I think there are infinite obfuscated ways. It depends what you
consider a "way" to print the integers 1 to 100. You can surround it
with unintelligible garbage, encrypt it and then unencrypt and eval
it, and so forth.

It's an interesting question: what's the horizon of a "way" to do
something, using "way" as in "Ruby gives you different ways to do
xyz"? I don't believe that the relation between, say, p *1..100 and
puts [1,2,3,...,100] is the same as the relation between either of
those ways of printing integers and

puts [*eval("1" + (("$%#$&*."[-1,1]) * 2) +
((212-32)*5/9).to_s)].pack(%{#{"".class.class.name[0].chr}*}).unpack("#{'B'.succ}*")

or whatever. That prints the numbers but I don't think it can usefully
be described as a "way" to print an array of integers.

David

···

On Mon, 5 May 2008, Marc Heiler wrote:

--
Rails training from David A. Black and Ruby Power and Light:
   INTRO TO RAILS June 9-12 Berlin
   ADVANCING WITH RAILS June 16-19 Berlin
   INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!

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

···

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://codeforpeople.com/
> > --
> > 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.huikau.com)

Different Ways To Loop

Posted by Wyatt Greene (Guest) on 04.05.2008 04:10

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. ...

Just for the sake of completeness!

puts Array(1..100)

Cheers,

j.k.

···

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

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

···

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://codeforpeople.com/
> > > --
> > > 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.huikau.com)

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

Hi --

[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

Don't forget the "traditional" style loop:

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

ara.t.howard 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://codeforpeople.com/
--
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.huikau.com)

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
loop do
number = 1
print_from_to(number, MAX)
break
end

I can think of lots of ways:

   puts *1..100
   puts *1..101-1
   puts *1..102-2 ...

or

   "This evaluates to true" and loop do ...

   "So does this" and loop do ...

:slight_smile:

I don't mean to scoff at the "more than one way" thing, but it does
in a sense contain the seeds of its own destruction :slight_smile:

David

···

On Sun, 4 May 2008, Ashley Wharton wrote:

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:
> On Sat, May 3, 2008 at 11:48 PM, Dan Zwell <dzw...@gmail.com> wrote:

On May 3, 2008, at 8:10 PM, Wyatt Greene wrote:

--
Rails training from David A. Black and Ruby Power and Light:
   INTRO TO RAILS June 9-12 Berlin
   ADVANCING WITH RAILS June 16-19 Berlin
   INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!

This discussion will self-destruct after 20 posts... :slight_smile:

···

On May 4, 9:34 am, "David A. Black" <dbl...@rubypal.com> wrote:

Hi --

On Sun, 4 May 2008, Ashley Wharton wrote:
> On Sun, May 4, 2008 at 8:55 AM, Wyatt Greene <green...@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://codeforpeople.com/
>>>>> --
>>>>> 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.huikau.com)

>> 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

I can think of lots of ways:

   puts *1..100
   puts *1..101-1
   puts *1..102-2 ...

or

   "This evaluates to true" and loop do ...

   "So does this" and loop do ...

:slight_smile:

I don't mean to scoff at the "more than one way" thing, but it does
in a sense contain the seeds of its own destruction :slight_smile:

David

--
Rails training from David A. Black and Ruby Power and Light:
   INTRO TO RAILS June 9-12 Berlin
   ADVANCING WITH RAILS June 16-19 Berlin
   INTRO TO RAILS June 24-27 London (Skills Matter)
Seehttp://www.rubypal.comfor details and updates!