[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: One line infinite for loop in Ruby?

Harold Hausman

9/6/2006 2:46:00 AM

On 9/5/06, Anil Wadghule <anildigital@gmail.com> wrote:
> Hi all,
>
> How to write one line infinte for loop in Ruby?
> I don't want loop{}.
>

How about:
while 1 do puts "OMG" end

hth,
-Harold

9 Answers

Arnaud Bergeron

9/6/2006 2:51:00 AM

0

On 9/5/06, Harold Hausman <hhausman@gmail.com> wrote:
> On 9/5/06, Anil Wadghule <anildigital@gmail.com> wrote:
> > Hi all,
> >
> > How to write one line infinte for loop in Ruby?
> > I don't want loop{}.
> >
>
> How about:
> while 1 do puts "OMG" end
>

Even better:

puts "OMG" while 1

--
"What is your function in life?" - Killer

darren kirby

9/6/2006 2:52:00 AM

0

quoth the Harold Hausman:
> On 9/5/06, Anil Wadghule <anildigital@gmail.com> wrote:
> > Hi all,
> >
> > How to write one line infinte for loop in Ruby?
> > I don't want loop{}.
>
> How about:
> while 1 do puts "OMG" end

I have been wondering, is there a no-op in Ruby? Then you could do like in
Python:

while 1: pass

> hth,
> -Harold

-d
--
darren kirby :: Part of the problem since 1976 :: http://badco...
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

Austin Ziegler

9/6/2006 3:02:00 AM

0

On 9/5/06, darren kirby <bulliver@badcomputer.org> wrote:
> I have been wondering, is there a no-op in Ruby? Then you could do like in
> Python:
>
> while 1: pass

nil while true

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

darren kirby

9/6/2006 3:25:00 AM

0

quoth the Austin Ziegler:
> On 9/5/06, darren kirby <bulliver@badcomputer.org> wrote:
> > I have been wondering, is there a no-op in Ruby? Then you could do like
> > in Python:
> >
> > while 1: pass
>
> nil while true

Cool, thanks.

I've just realized you don't even need the 'nil' to flesh out some
syntactically valid functions/methods/case statements that do nothing:

irb(main):005:0> def foobar
irb(main):006:1> end
=> nil

irb(main):010:0> x = 1
irb(main):011:0> if x == 2
irb(main):012:1> puts "Two!"
irb(main):013:1> elsif x == 1 # do nothing
irb(main):014:1> else
irb(main):015:1* puts "Not 1 or 2"
irb(main):016:1> end
=> nil

technically they return nil but that is incidental...

> -austin

-d
--
darren kirby :: Part of the problem since 1976 :: http://badco...
"...the number of UNIX installations has grown to 10, with more expected..."
- Dennis Ritchie and Ken Thompson, June 1972

Devin Mullins

9/6/2006 3:26:00 AM

0

>> I have been wondering, is there a no-op in Ruby? Then you could do
>> while 1: pass
> nil while true
while true; end

...wait. Why are we doing this? And why are we disallowed from using
loop{}? Okay, here's another:
alias repeat loop; repeat{}
Or:
(1..1.0/0)..each{}
How about:
callcc{|$cc|};$cc[]

Shall I continue, Anil?

Devin

Logan Capaldo

9/6/2006 3:49:00 AM

0


On Sep 5, 2006, at 11:26 PM, Devin Mullins wrote:

>
> ...wait. Why are we doing this? And why are we disallowed from
> using loop{}? Okay, here's another:
> alias repeat loop; repeat{}
> Or:
> (1..1.0/0)..each{}
> How about:
> callcc{|$cc|};$cc[]
>
> Shall I continue, Anil?
> Devin
>

Heres another one:

(a = lambda { a.call }).call

Of course this is one infinite loop that will probably terminate.

Joel VanderWerf

9/6/2006 4:35:00 AM

0

Austin Ziegler wrote:
> On 9/5/06, darren kirby <bulliver@badcomputer.org> wrote:
>> I have been wondering, is there a no-op in Ruby? Then you could do
>> like in
>> Python:
>>
>> while 1: pass
>
> nil while true

Golfing slightly and not very interestingly:

0while 0

(that's not a syntax error, oddly)

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Devin Mullins

9/6/2006 5:18:00 AM

0

Anil Wadghule wrote:
> On 9/6/06, Devin Mullins <twifkak@comcast.net> wrote:
>> Shall I continue, Anil?> Yes! You can continue!
Err... okay, I can think of another:
raise rescue retry

Wow... that one's pretty elegant. This one much less so:
1.times { redo }

I'm out... maybe something cute with threads, or throw/catch, or ensure?

Is there a reason for your question? Just a ruby-talk game?

Devin

Mike Dvorkin

9/6/2006 5:33:00 AM

0

Here's an ultimate one, just to make sure :-)

while 1
nil while 1 && 1.times {redo} && raise rescue retry
end

Best,
Mike Dvorkin
http://www.rubyw...