[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Infinate Loop - Please Advise

John Joyce

4/9/2007 4:31:00 PM

here's another example, using downto


beer = 99
beer.downto(0) do |b|
puts "#{b} bottles of beer."
puts "take one, drink it."
puts "#{b - 1} left."
end

but that needs an if statement to take care of the negative beer!

beer = 99
beer.downto(0) do |b|
puts "#{b} bottles of beer."
if b > 0 do
puts "take one, drink it."
puts "#{b - 1} left."
end
end


Of course it could be shorter still. but that's for you to toy with.

7 Answers

Raj Sahae

4/9/2007 6:04:00 PM

0

John Joyce wrote:
> here's another example, using downto
>
>
> beer = 99
> beer.downto(0) do |b|
> puts "#{b} bottles of beer."
> puts "take one, drink it."
> puts "#{b - 1} left."
> end
>
> but that needs an if statement to take care of the negative beer!
>
> beer = 99
> beer.downto(0) do |b|
> puts "#{b} bottles of beer."
> if b > 0 do
> puts "take one, drink it."
> puts "#{b - 1} left."
> end
> end
>
>
> Of course it could be shorter still. but that's for you to toy with.
>
>
why wouldn't you just have beer.downto(1) and ditch the if statement?

John Joyce

4/9/2007 6:09:00 PM

0

Sorry, here's a better working example. I wrote the others quickly in
irb. The trouble with irb is that it isn't the most graceful
environment for writing nested conditions or loops or elements. It's
possible, but easy to make mistakes. Anyway, the white space in the
conditionals and looping constructs is pretty important to Ruby.

beer = 99
beer.downto(1) do |cerveza|
puts "#{cerveza} bottles of beer on the wall,"
puts "#{cerveza} bottles of beer..."
puts "take one down pass it around..."
if cerveza > 1
puts "#{cerveza - 1} bottles of beer on the wall"
else
puts "no mas cerveza..."
end
end



John Joyce

4/9/2007 6:18:00 PM

0


On Apr 10, 2007, at 3:03 AM, Raj Sahae wrote:

> John Joyce wrote:
>> here's another example, using downto
>>
>>
>> beer = 99
>> beer.downto(0) do |b|
>> puts "#{b} bottles of beer."
>> puts "take one, drink it."
>> puts "#{b - 1} left."
>> end
>>
>> but that needs an if statement to take care of the negative beer!
>>
>> beer = 99
>> beer.downto(0) do |b|
>> puts "#{b} bottles of beer."
>> if b > 0 do
>> puts "take one, drink it."
>> puts "#{b - 1} left."
>> end
>> end
>>
>>
>> Of course it could be shorter still. but that's for you to toy with.
>>
>>
> why wouldn't you just have beer.downto(1) and ditch the if statement?

I did, a few moments later. doing that stuff in irb isn't always as
easy as doing it in TextMate (note to self).
I like irb because I can just ask objects questions. But irb needs
more flexibility (the ability to step into and out of writing a block
to check something else, that would be useful) Access to all the
running stuff and ability to change it while running. Oh.
self.assend_to_heaven
>


Ash

4/10/2007 12:50:00 AM

0

On Apr 9, 2:17 pm, John Joyce <dangerwillrobinsondan...@gmail.com>
wrote:
> - snip -
> I did, a few moments later. doing that stuff in irb isn't always as
> easy as doing it in TextMate (note to self).
> I like irb because I can just ask objects questions. But irb needs
> more flexibility (the ability to step into and out of writing a block
> to check something else, that would be useful) Access to all the
> running stuff and ability to change it while running. Oh.
> self.assend_to_heaven

So basically, you want Smalltalk :-)

If you're unfamiliar, Smalltalk lives in an 'image' which offers the
flexibility you describe, with a full-GUI IDE environment -- you can
inspect and manipulate live objects and code as you develop, test and
debug.

I've actually considered building a similar environment for Ruby,
should I find the time and as wxRuby becomes more stable. As a matter
of fact, I believe I remember reading on here that FreeRIDE was
developed with that particular aim in mind.

On one hand it seems superfluous, since Smalltalk already has one, but
on the other it would be a great deal of fun to do so, and it seems
like a fit with the language given Ruby's Smalltalk heritage.

John Joyce

4/10/2007 3:55:00 AM

0

Yeah! A SmallTalk-like environment would be wonderful.
irb is already pretty cool.
But it always seems to be just short of what it could be.
I kinda wish TextMate had its own irb session ability, then it could
be in color and have lots of cool tricks too.
But these are dreams and wishes, because I am not about to write
anything like this myself. Not that clever.

On Apr 10, 2007, at 9:50 AM, Ash wrote:

> On Apr 9, 2:17 pm, John Joyce <dangerwillrobinsondan...@gmail.com>
> wrote:
>> - snip -
>> I did, a few moments later. doing that stuff in irb isn't always as
>> easy as doing it in TextMate (note to self).
>> I like irb because I can just ask objects questions. But irb needs
>> more flexibility (the ability to step into and out of writing a block
>> to check something else, that would be useful) Access to all the
>> running stuff and ability to change it while running. Oh.
>> self.assend_to_heaven
>
> So basically, you want Smalltalk :-)
>
> If you're unfamiliar, Smalltalk lives in an 'image' which offers the
> flexibility you describe, with a full-GUI IDE environment -- you can
> inspect and manipulate live objects and code as you develop, test and
> debug.
>
> I've actually considered building a similar environment for Ruby,
> should I find the time and as wxRuby becomes more stable. As a matter
> of fact, I believe I remember reading on here that FreeRIDE was
> developed with that particular aim in mind.
>
> On one hand it seems superfluous, since Smalltalk already has one, but
> on the other it would be a great deal of fun to do so, and it seems
> like a fit with the language given Ruby's Smalltalk heritage.
>
>


James Gray

4/10/2007 12:35:00 PM

0

On Apr 9, 2007, at 10:54 PM, John Joyce wrote:

> I kinda wish TextMate had its own irb session ability, then it
> could be in color and have lots of cool tricks too.

Two things:

1. TextMate does ship with xmpfilter wrapped in a command, so it's
already possible to evaluate statements and see the results, while
keeping all the editing advantages of TextMate. See Bundles -> Ruby -
> Execute and Update '# =>' Markers.

2. I wrote an article about how to build interactive environments
inside of TextMate. The techniques used in it, could be used to make
an "IRbMate." It should be published next week on http://
www.macdevcenter.com/.

James Edward Gray II


John Joyce

4/10/2007 1:32:00 PM

0


On Apr 10, 2007, at 9:34 PM, James Edward Gray II wrote:

> On Apr 9, 2007, at 10:54 PM, John Joyce wrote:
>
>> I kinda wish TextMate had its own irb session ability, then it
>> could be in color and have lots of cool tricks too.
>
> Two things:
>
> 1. TextMate does ship with xmpfilter wrapped in a command, so it's
> already possible to evaluate statements and see the results, while
> keeping all the editing advantages of TextMate. See Bundles ->
> Ruby -> Execute and Update '# =>' Markers.
>
> 2. I wrote an article about how to build interactive environments
> inside of TextMate. The techniques used in it, could be used to
> make an "IRbMate." It should be published next week on http://
> www.macdevcenter.com/.
>
> James Edward Gray II
>
>

look forward to seeing it.
Kinda waiting to see what happens with the next version of TextMate
too. Needs better multilingual support. This is the only thing I can
really really be disappointed about in TM. Quite ironic that the
premier Ruby and Rails editor in the west will barf on Japanese
input, and that it is such a wonderful example of a good Mac app yet
doesn't have the language support so well provided by Cocoa...
now we're way OT