[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Counting Program

Will Mueller

4/20/2008 9:22:00 PM

I recently actually started "studying" ruby again and attempted to
create a program that would add numbers and print them out (on the
screen of course). Of course the following code would not work:

time1=1
10000000.times do
puts time1+1
end

All it would do, is simply print out the number 2, 10000000 times. I
continually got errors when editing it. Could someone please assist?

With Thanks,
--
Will
wce.page.tl
freetechtips.tk
will.liljon@gmail.com

2 Answers

Phillip Gawlowski

4/20/2008 9:34:00 PM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Will Mueller wrote:

| time1=1
| 10000000.times do
| puts time1+1
| end
|
| All it would do, is simply print out the number 2, 10000000 times. I
| continually got errors when editing it. Could someone please assist?

Well, it's as simple as adding a '=':

time1 = 1
10000000.times do
~ time1 += 1 # this is shorthand for time1 = time1 + 1
~ puts time1
end

You need to tell Ruby, that you want to change the variable explicitly.

A quote I cannot attribute: "Computers never do what they should, only
what you tell them to do". ;)

It might help if you put spaces between operators, as that helps in
catching errors. I am speaking from experience.

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan

~ "That's the whole problem with science. You've got a bunch of
~ empiricists trying to describe things of unimaginable wonder."
~ --- Calvin
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail....

iEYEARECAAYFAkgLtsIACgkQbtAgaoJTgL/rYQCgkPvb3NX47KpkP+6uXdvxpGws
Tn8AnAqS0RyN0aa2t3QPvxNem58Bi/bj
=AFlL
-----END PGP SIGNATURE-----

Will Mueller

4/20/2008 10:14:00 PM

0

Thanks, that worked.

On Sun, Apr 20, 2008 at 4:33 PM, Phillip Gawlowski
<cmdjackryan@googlemail.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>
> Will Mueller wrote:
>
> | time1=1
> | 10000000.times do
> | puts time1+1
> | end
> |
> | All it would do, is simply print out the number 2, 10000000 times. I
> | continually got errors when editing it. Could someone please assist?
>
> Well, it's as simple as adding a '=':
>
>
> time1 = 1
> 10000000.times do
> ~ time1 += 1 # this is shorthand for time1 = time1 + 1
> ~ puts time1
> end
>
> You need to tell Ruby, that you want to change the variable explicitly.
>
> A quote I cannot attribute: "Computers never do what they should, only
> what you tell them to do". ;)
>
> It might help if you put spaces between operators, as that helps in
> catching errors. I am speaking from experience.
>
> - --
> Phillip Gawlowski
> Twitter: twitter.com/cynicalryan
>
> ~ "That's the whole problem with science. You've got a bunch of
> ~ empiricists trying to describe things of unimaginable wonder."
> ~ --- Calvin
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.8 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail....
>
> iEYEARECAAYFAkgLtsIACgkQbtAgaoJTgL/rYQCgkPvb3NX47KpkP+6uXdvxpGws
> Tn8AnAqS0RyN0aa2t3QPvxNem58Bi/bj
> =AFlL
> -----END PGP SIGNATURE-----
>
>



--
Will M
wce.page.tl
freetechtips.tk
will.liljon@gmail.com