Simple question

I have just picked up ruby and am trying to teach my self. so this
question is probably easy but its giving me fits.

class Test
def round
puts “the object is round”
end
def random
$radus=rand(10)+1 <—Global Variable
end
def coords
x=rand(1000)
y=rand(1000)
z=rand(1000)
lo=[x,y,z]
puts lo
end
def size
pi=Math::PI
area=pi*(radus^2) <----error cant find radus?
volume = 4/3pi(radus^3)
puts volume
puts radus
puts area
end
def landmass
landmass=size.area*0.33 <— why is this wrong?
puts landmass
end

t=Test.new
puts t.landmass
end

other than my ugly code; why doesn’t size see radus and landmass see
area

Subject: Simple question
Date: Sat 03 Nov 12 03:52:00AM +0900

Quoting August Y. ([email protected]):

def coords
x=rand(1000)
y=rand(1000)
z=rand(1000)
lo=[x,y,z]
puts lo
end
def size
pi=Math::PI
area=pi*(radus^2) <----error cant find radus?

If you want to refer to your global variable, you must always use the
dollar sign:

 area=pi*($radus^2)

If you just use ‘radus’, you refer to a different variable, which in
this case is not defined. The scope of variables whose first character
is a lower-case letter is limited to the method body.

volume = 4/3*pi*(radus^3)
puts volume
puts radus
puts area

end
def landmass
landmass=size.area*0.33 <— why is this wrong?

size is a method. If you want the method to return the area, you must
rewrite it as

def size
pi=Math::PI
area=pi*($radus^2) <----error cant find radus?
volume = 4/3pi($radus^3)
puts volume
puts $radus
puts area

 return area

end

and then you can write the line as

 landmass=size()*0.33

which means: assign to variable landmass the return value of methods
size, multiplied by 0.33.

puts landmass

this line just prints the value on screen, but you want to return the
value. You must add

  return landmass

If you do not specify an explicit return statement, methods return the
result of the last operation that has been executed. The #puts method
returns nil, so your two methods return nil.

Note that it is not the best thing to use a global variable. Since the
variable is accessed only within the instance methods of a class, it is
more appropriate to use a class instance variable (in all places,
write @radus instead of $radus).

end

t=Test.new
puts t.landmass
end

You have to understand better the concepts of classes, instances,
methods, There are dozens of different ways of doing things in Ruby,
but I would write your class by using class instance variables for
radus, area, volume, landmass.

Try again!

Carlo

This might help
http://www.techotopia.com/index.php/Ruby_Variable_Scope

this block
def size
pi=Math::PI
area=pi*($radus^2)
volume = 4/3pi($radus^3)
return volume
return $radus
return area
end

Returns this error
C:/Users/jyoung/objects.rb:20:in *': true can't be coerced into Float (TypeError) from C:/Users/jyoung/objects.rb:20:insize’
from C:/Users/jyoung/objects.rb:31:in <class:Test>' from C:/Users/jyoung/objects.rb:4:in

pi is defigned and $radus is to.f so what am i missing

thanks

Subject: Re: Simple question
Date: Mon 05 Nov 12 10:52:15PM +0900

Quoting August Y. ([email protected]):

Returns this error
C:/Users/jyoung/objects.rb:20:in *': true can't be coerced into Float (TypeError) from C:/Users/jyoung/objects.rb:20:insize’
from C:/Users/jyoung/objects.rb:31:in <class:Test>' from C:/Users/jyoung/objects.rb:4:in

pi is defigned and $radus is to.f so what am i missing

^ is the exclusive-or operator. It returns a boolean value. Which
cannot be multiplied by a float.

It looks like you want the power operator, which is **

 area=pi*($radus**2)
 volume=4/3*pi*($radus**3)

Carlo

Am 05.11.2012 15:14, schrieb Carlo E. Prelz:

Subject: Re: Simple question
Date: Mon 05 Nov 12 10:52:15PM +0900

Quoting August Y. ([email protected]):

 area=pi*($radus^2)

C:/Users/jyoung/objects.rb:20:in `*’: true can’t be coerced into Float

pi is defigned and $radus is to.f so what am i missing

^ is the exclusive-or operator. It returns a boolean value. Which
cannot be multiplied by a float.

depends on $radus, which is probably nil.

For an Integer int^2 would return an Integer,
and for a Float there would be a NoMethodError.

But: nil^2 => true

Am 05.11.2012 14:52, schrieb August Y.:

Returns this error
C:/Users/jyoung/objects.rb:20:in `*': true can’t be coerced into Float
(TypeError)

You are trying to multiply with true' in line 20 of your program. You could add a line before line 20 with puts $radius’ to see
whether the variable really has the value you think it has.

pi is defigned and $radus is to.f so what am i missing

you mean to_f ?

Furthermore:

  • ^' does not do what you think it does -> bitwise xor!!! You need to use ’ (32 = 9)
  • 4/3 is 1 (integer division), you need 4.0 / 3 = 1.3333333
  • only the first return statement is executed,
    the last two lines are never reached.

You should read up on some basics, see e.g.
http://pine.fm/LearnToProgram/.