Counting Program

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

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". :wink:

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

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". :wink:

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

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

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 }

Dan Diebolt wrote:

···

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?

···

On Wed, Apr 23, 2008 at 2:15 PM, Dana Merrick <dana.merrick@trincoll.edu> wrote:

Or even use a range:

(1..10).each { |num| p num }

Dan Diebolt wrote:

>
> > it's as simple as ...
> >
>
> adding an argument list:
>
> 10.times do |time1|
> puts time1+1
> end
>
>

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

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!

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

I didn't know that! That's awesome.

-Dana

David A. Black 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

require 'readline'

num = ask('Count to what number?: )

num.times do |x|
  puts x
  sleep 1
  x += 1
end

end

···

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.

edit:

···

require 'highline'

num = ask('Count to what number?: )

num.times do |x|
  puts x
  sleep 1
  x += 1
end

end

Received the following error: realcountin.rb:3 unterminated string
meets end of file

realcounting.rb:3: syntax error, unexpected $end, expecting ')'

···

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

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

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.

···

On Fri, Apr 25, 2008 at 4:47 PM, Will Mueller <will.liljon@gmail.com> wrote:

Received the following error: realcountin.rb:3 unterminated string
meets end of file

realcounting.rb:3: syntax error, unexpected $end, expecting ')'

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.

···

--
Posted via http://www.ruby-forum.com/.

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.

The previous one worked. Thanks.

···

On Fri, Apr 25, 2008 at 5:35 PM, John <john.d.perkins@gmail.com> wrote:

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.

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

I suppose if we're golfing:

p "Upto what number?"; p *1..gets.to_i

-Dana

Peter Bunyan wrote:

···

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.