Logan Capaldo
5/22/2007 2:24:00 AM
On 5/21/07, Hakusa@gmail.com <Hakusa@gmail.com> wrote:
> I have some good experience in other languages and consider myself a
> good programmer, but some of the good techniques I've picked up don't
> seem to work here. Here are two examples.
>
> x=aNum
> y=0
> x.times do
> (y+1).times do
> Stuff
> end
> end
>
That's not how times works,
a_num.times do |x|
p x
end
> y never actually increments. Why? (No pun intended.) y should go up
> one x times by the end. Instead it stays at zero. And it's kind of
> troubling that is does the loop once even when y is zero; or is that
> just because it's a 'do' loop and not a 'for' loop?
You said do the following y + 1 times.
y is zero, y + 1 is 1. therefore, execute the following block of code, once.
Other methods you may be interested in are:
0.upto(10) do |y|
p y
end
or
0.step(5, 1) do |y|
p y
end
>
>
> And I've learned that to do the gets thing, I have to have
> STDOUT.flush before it. It seems that no matter what I do, if it's
> even two lines above the gets, I get the gets at the start of the
> program even though it was towards the end. Why? If it is such a
> common thing, why is STDOUT.flush not automatically called by the
> language?
>
>
> Although as much as this all complexes me, I am very glad to be able
> to do this with guilty glee:
>
> STDOUT.flush
> puts 'Hello ' + gets.chomp
>
Look what you've written:
puts( 'Hello' + gets.chomp )
That means it must ask the user for input before it can create the
string to print.
> Although I have a feeling I'll rarely get to use this trick.
>
>
>
I would suggest you look into a ruby tutorial or book such as
Programming Ruby 2nd Ed. or Chris Pine's Learn to Program.