a program error

T

Teodor Carstea

hi, all! my compiler doesn't like the "minus" in line 21, it's like:

/home/cristi/NetBeansProjects/RubyApplication3/lib/helic.rb:21:
undefined method `-' for "":String (NoMethodError)

1 require "init.rb"
2
3 shots = 3
4 time = 1
5 speed = 0
6 x=y=1
7 while (shots!=0) do
8 #give entry data
9 puts "let's f*ck da su**er!"
10 puts "give speed:"
11 speed = gets
12 puts "give angle:"
13 angle = gets
14 #processing entry data
15 angle=(Math::pI)/180*angle.to_f
16 xspeed=speed*Math.cos(angle)
17 yspeed=speed*Math.sin(angle)
18 #trajectory's loop
19 while (y>0) do
20 x=xspeed*time
21 y=yspeed*time-4.9*time**2
22 time+=0.01
23
24 #puts "x="+x.to_i.to_s+" y="+y.to_i.to_s
25 screen.fill_rect 0, 0, 640, 480, BGCOLOR
26 screen.draw_circle x, y, 5, SHELLCOLOR
27 screen.flip
28
29 end
30 shots-=1
31 puts "shots remaining"+ shots.to_s
32 end

help, please. thanks!
 
Z

Zhao Lu

it appears that yspeed is "", which implies speed from gets is not what =
you expect it to be. puts speed after you gets it and see.
 
D

Dasson, Raghav

Try to use=20
speed =3D gets.to_i at line 11 and
angle =3D gets.to_i at line 13 to resolve this.

Regards,
Raghav

-----Original Message-----
From: (e-mail address removed) [mailto:[email protected]]=20
Sent: Wednesday, December 16, 2009 1:08 PM
To: ruby-talk ML
Subject: a program error

hi, all! my compiler doesn't like the "minus" in line 21, it's like:

/home/cristi/NetBeansProjects/RubyApplication3/lib/helic.rb:21:
undefined method `-' for "":String (NoMethodError)

1 require "init.rb"
2
3 shots =3D 3
4 time =3D 1
5 speed =3D 0
6 x=3Dy=3D1
7 while (shots!=3D0) do
8 #give entry data
9 puts "let's f*ck da su**er!"
10 puts "give speed:"
11 speed =3D gets
12 puts "give angle:"
13 angle =3D gets
14 #processing entry data
15 angle=3D(Math::pI)/180*angle.to_f
16 xspeed=3Dspeed*Math.cos(angle)
17 yspeed=3Dspeed*Math.sin(angle)
18 #trajectory's loop
19 while (y>0) do
20 x=3Dxspeed*time
21 y=3Dyspeed*time-4.9*time**2
22 time+=3D0.01
23
24 #puts "x=3D"+x.to_i.to_s+" y=3D"+y.to_i.to_s
25 screen.fill_rect 0, 0, 640, 480, BGCOLOR
26 screen.draw_circle x, y, 5, SHELLCOLOR
27 screen.flip
28
29 end
30 shots-=3D1
31 puts "shots remaining"+ shots.to_s
32 end

help, please. thanks!
--=20
Posted via http://www.ruby-forum.com/.
 
T

Teodor Carstea

Zhao said:
it appears that yspeed is "", which implies speed from gets is not what
you expect it to be. puts speed after you gets it and see.

The answer is

speed = gets.to_s

puts speed gave integer, but puts xspeed(and yspeed too) gave "". You
were about right. I fell so stupid...

Thanks!
 
B

Bertram Scharpf

Hi,

Am Mittwoch, 16. Dez 2009, 16:38:07 +0900 schrieb Teodor Carstea:
hi, all! my compiler doesn't like the "minus" in line 21, it's like:

/home/cristi/NetBeansProjects/RubyApplication3/lib/helic.rb:21:
undefined method `-' for "":String (NoMethodError)

[...]
20 x=xspeed*time
21 y=yspeed*time-4.9*time**2
[...]

Put something like this before line 21 and the cause of the error
is obvious:

puts [ xspeed, yspeed, time].inspect

Bertram
 

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
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top