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.
<code>
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/3*pi*(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
</code>

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

···

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

Quoting August Y. (lists@ruby-forum.com):

I have just picked up ruby and am trying to teach my self. so this
question is probably easy but its giving me fits.
<code>
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?

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/3*pi*($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
</code>

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

···

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

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

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

···

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

this block
def size
    pi=Math::PI
    area=pi*($radus^2)
    volume = 4/3*pi*($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:in `size'
  from C:/Users/jyoung/objects.rb:31:in `<class:Test>'
  from C:/Users/jyoung/objects.rb:4:in `<main>'

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

···

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

Quoting August Y. (lists@ruby-forum.com):

this block
def size
    pi=Math::PI
    area=pi*($radus^2)
    volume = 4/3*pi*($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:in `size'
  from C:/Users/jyoung/objects.rb:31:in `<class:Test>'
  from C:/Users/jyoung/objects.rb:4:in `<main>'

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

···

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

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

this block
def size
     pi=Math::PI
     area=pi*($radus^2)
     volume = 4/3*pi*($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)

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 `**' (3**2 = 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/&gt;\.

···

Am 05.11.2012 14:52, schrieb August Y.:

--
<https://github.com/stomar/&gt;

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 15:14, schrieb Carlo E. Prelz:

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

Quoting August Y. (lists@ruby-forum.com):

     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.

--
<https://github.com/stomar/&gt;