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?
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.
~ "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
--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!
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.
···
On Fri, Apr 25, 2008 at 7:03 AM, David A. Black <dblack@rubypal.com> wrote:
Hi --
On Thu, 24 Apr 2008, Dana Merrick wrote:
> Dan Diebolt wrote:
>
> >
> > > it's as simple as ...
> > >
> >
> > adding an argument list:
> >
> > 10.times do |time1|
> > puts time1+1
> > end
> >
> >
>
> 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
--
Rails training from David A. Black and Ruby Power and Light:
INTRO TO RAILS June 9-12 Berlin
ADVANCING WITH RAILS June 16-19 Berlin
INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!
--
Will M
wce.page.tl
freetechtips.tk
will.liljon@gmail.com
On Fri, Apr 25, 2008 at 3:20 PM, John <john.d.perkins@gmail.com> wrote:
On Apr 25, 10:08 am, Will Mueller <will.lil...@gmail.com> 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
--
Will M
wce.page.tl
freetechtips.tk
will.liljon@gmail.com
On Fri, Apr 25, 2008 at 3:20 PM, John <john.d.perkins@gmail.com> wrote:
> On Apr 25, 10:08 am, Will Mueller <will.lil...@gmail.com> 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
>
>
--
Will M
wce.page.tl
freetechtips.tk
will.liljon@gmail.com
--
Will M
wce.page.tl
freetechtips.tk
will.liljon@gmail.com
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.
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.