Counting Program

W

Will Mueller

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

Phillip Gawlowski

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

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

Will Mueller

Thanks, that worked.

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

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

Peter Bunyan

print "Upto what number? " # ask the question
max = gets.to_i # get a string and convert it to a number
puts *(1..max) # creates a range from 1 to max, then turns it into an
array, then prints each one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,206
Messages
2,571,070
Members
47,676
Latest member
scazeho

Latest Threads

Top