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,
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Will M. 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 G.
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-----
Thanks, that worked.
On Sun, Apr 20, 2008 at 4:33 PM, Phillip G.
Or even use a range:
(1…10).each { |num| p num }
it’s as simple as …
adding an argument list:
10.times do |time1|
puts time1+1
end
If I was to revise it to be able to simply type in a number and then
allow the program to count up to that on the screen how should it be
revised?
time2=(something with user input)
puts time2+=1
end
Something like that?
Hi –
On Thu, 24 Apr 2008, Dana M. wrote:
Or even use a range:
(1…10).each { |num| p num }
If you really just want to print out the numbers:
puts *1…10
David
On Apr 25, 10:08 am, Will M. [email protected] wrote:
No, I mean in a manner that allows user input. So you may type in a
number then allow the computer to count to that number.
require ‘readline’
num = ask('Count to what number?: )
num.times do |x|
puts x
sleep 1
x += 1
end
end
No, I mean in a manner that allows user input. So you may type in a
number then allow the computer to count to that number.
Received the following error: realcountin.rb:3 unterminated string
meets end of file
realcounting.rb:3: syntax error, unexpected $end, expecting ‘)’
Moral of the story: run your code before they all laugh at you.
require ‘rubygems’
require ‘highline/import’
count = ask('Count to what number?: ')
c = 0
amt = count.to_i + 1
amt.times do |c|
puts c
c += 1
sleep 1
end
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.
How are you running this? My snippet works from the command line, I
don’t know about irb. Save it as ‘countly.rb’ and try >ruby countly.rb
Interesting.
Still getting an error after editing to make:
require ‘rubygems’
require ‘highline/import’
count = ask('Count to what number?: ')
c = 0
amt = count.to_i + 1
amt.times do |c|
puts c
c += 1
sleep 1
end
Much larger error.
I suppose if we’re golfing:
p “Upto what number?”; p *1…gets.to_i
-Dana
The previous one worked. Thanks.
I didn’t know that! That’s awesome.
-Dana