# Simple question

**URL:** <https://rubytalk.org/t/simple-question/66608>\
**Category:** ruby-talk\
**Created:** [2 November 2012 18:52 UTC](https://rubytalk.org/t/simple-question/66608 "2012-11-02T18:52:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [2 November 2012 18:52 UTC](https://rubytalk.org/t/simple-question/66608/1 "2012-11-02T18:52:00Z")

</div>

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  
&nbsp;&nbsp;def round  
&nbsp;&nbsp;&nbsp;&nbsp;puts "the object is round"  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def random  
&nbsp;&nbsp;&nbsp;&nbsp;$radus=rand(10)+1 \<---Global Variable  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def coords  
&nbsp;&nbsp;&nbsp;&nbsp;x=rand(1000)  
&nbsp;&nbsp;&nbsp;&nbsp;y=rand(1000)  
&nbsp;&nbsp;&nbsp;&nbsp;z=rand(1000)  
&nbsp;&nbsp;&nbsp;&nbsp;lo=[x,y,z]  
&nbsp;&nbsp;&nbsp;&nbsp;puts lo  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def size  
&nbsp;&nbsp;&nbsp;&nbsp;pi=Math::PI  
&nbsp;&nbsp;&nbsp;&nbsp;area=pi\*(radus^2) \<----error cant find radus?  
&nbsp;&nbsp;&nbsp;&nbsp;volume = 4/3\*pi\*(radus^3)  
&nbsp;&nbsp;&nbsp;&nbsp;puts volume  
&nbsp;&nbsp;&nbsp;&nbsp;puts radus  
&nbsp;&nbsp;&nbsp;&nbsp;puts area  
&nbsp;&nbsp;end  
&nbsp;&nbsp;def landmass  
&nbsp;&nbsp;&nbsp;&nbsp;landmass=size.area\*0.33 \<--- why is this wrong?  
&nbsp;&nbsp;&nbsp;&nbsp;puts landmass  
&nbsp;&nbsp;end

&nbsp;&nbsp;t=Test.new  
&nbsp;&nbsp;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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Carlo\_E\_Prelz](https://avatars.discourse-cdn.com/v4/letter/c/dbc845/32.png) [@Carlo\_E\_Prelz](https://rubytalk.org/u/Carlo_E_Prelz)\
**Post date:** [2 November 2012 19:24 UTC](https://rubytalk.org/t/simple-question/66608/2 "2012-11-02T19:24:22Z")

</div>

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  
> &nbsp;&nbsp;def round  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "the object is round"  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;def random  
> &nbsp;&nbsp;&nbsp;&nbsp;$radus=rand(10)+1 \<---Global Variable  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;def coords  
> &nbsp;&nbsp;&nbsp;&nbsp;x=rand(1000)  
> &nbsp;&nbsp;&nbsp;&nbsp;y=rand(1000)  
> &nbsp;&nbsp;&nbsp;&nbsp;z=rand(1000)  
> &nbsp;&nbsp;&nbsp;&nbsp;lo=[x,y,z]  
> &nbsp;&nbsp;&nbsp;&nbsp;puts lo  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;def size  
> &nbsp;&nbsp;&nbsp;&nbsp;pi=Math::PI  
> &nbsp;&nbsp;&nbsp;&nbsp;area=pi\*(radus^2) \<----error cant find radus?

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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.

> &nbsp;&nbsp;&nbsp;&nbsp;volume = 4/3\*pi\*(radus^3)  
> &nbsp;&nbsp;&nbsp;&nbsp;puts volume  
> &nbsp;&nbsp;&nbsp;&nbsp;puts radus  
> &nbsp;&nbsp;&nbsp;&nbsp;puts area  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;def landmass  
> &nbsp;&nbsp;&nbsp;&nbsp;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

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return area  
&nbsp;&nbsp;&nbsp;end

and then you can write the line as

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;landmass=size()\*0.33

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

> &nbsp;&nbsp;&nbsp;&nbsp;puts landmass

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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).

> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;t=Test.new  
> &nbsp;&nbsp;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  
> &nbsp;&nbsp;Date: Sat 03 Nov 12 03:52:00AM +0900
> 
> --  
> &nbsp;&nbsp;\* Se la Strada e la sua Virtu' non fossero state messe da parte,  
> \* K \* Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe  
> &nbsp;&nbsp;\* di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [2 November 2012 19:26 UTC](https://rubytalk.org/t/simple-question/66608/3 "2012-11-02T19:26:58Z")

</div>

This might help  
[http://www.techotopia.com/index.php/Ruby\_Variable\_Scope](http://www.techotopia.com/index.php/Ruby_Variable_Scope)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [5 November 2012 13:52 UTC](https://rubytalk.org/t/simple-question/66608/4 "2012-11-05T13:52:15Z")

</div>

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

Returns this error  
C:/Users/jyoung/objects.rb:20:in `\*': true can't be coerced into Float  
(TypeError)  
&nbsp;&nbsp;from C:/Users/jyoung/objects.rb:20:in `size'  
&nbsp;&nbsp;from C:/Users/jyoung/objects.rb:31:in `\<class:Test\>'  
&nbsp;&nbsp;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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Carlo\_E\_Prelz](https://avatars.discourse-cdn.com/v4/letter/c/dbc845/32.png) [@Carlo\_E\_Prelz](https://rubytalk.org/u/Carlo_E_Prelz)\
**Post date:** [5 November 2012 14:14 UTC](https://rubytalk.org/t/simple-question/66608/5 "2012-11-05T14:14:12Z")

</div>

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

> this block  
> def size  
> &nbsp;&nbsp;&nbsp;&nbsp;pi=Math::PI  
> &nbsp;&nbsp;&nbsp;&nbsp;area=pi\*($radus^2)  
> &nbsp;&nbsp;&nbsp;&nbsp;volume = 4/3\*pi\*($radus^3)  
> &nbsp;&nbsp;&nbsp;&nbsp;return volume  
> &nbsp;&nbsp;&nbsp;&nbsp;return $radus  
> &nbsp;&nbsp;&nbsp;&nbsp;return area  
> &nbsp;&nbsp;end
> 
> Returns this error  
> C:/Users/jyoung/objects.rb:20:in `\*': true can't be coerced into Float  
> (TypeError)  
> &nbsp;&nbsp;from C:/Users/jyoung/objects.rb:20:in `size'  
> &nbsp;&nbsp;from C:/Users/jyoung/objects.rb:31:in `\<class:Test\>'  
> &nbsp;&nbsp;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 \*\*

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;area=pi\*($radus\*\*2)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;volume=4/3\*pi\*($radus\*\*3)

Carlo

> **···**
>
> Subject: Re: Simple question  
> &nbsp;&nbsp;Date: Mon 05 Nov 12 10:52:15PM +0900
> 
> --  
> &nbsp;&nbsp;\* Se la Strada e la sua Virtu' non fossero state messe da parte,  
> \* K \* Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe  
> &nbsp;&nbsp;\* di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [5 November 2012 14:16 UTC](https://rubytalk.org/t/simple-question/66608/6 "2012-11-05T14:16:52Z")

</div>

> this block  
> def size  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pi=Math::PI  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;area=pi\*($radus^2)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;volume = 4/3\*pi\*($radus^3)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return volume  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $radus  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return area  
> &nbsp;&nbsp;&nbsp;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!!!  
&nbsp;&nbsp;&nbsp;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,  
&nbsp;&nbsp;&nbsp;the last two lines are never reached.

You should read up on some basics, see e.g.  
\<[http://pine.fm/LearnToProgram/&gt;\](http://pine.fm/LearnToProgram/&gt;%5C).

> **···**
>
> Am 05.11.2012 14:52, schrieb August Y.:
> 
> --  
> \<[https://github.com/stomar/&gt](https://github.com/stomar/&gt);

---

<div class="post-metadata">

**Author:** ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)\
**Post date:** [5 November 2012 14:25 UTC](https://rubytalk.org/t/simple-question/66608/7 "2012-11-05T14:25:05Z")

</div>

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:
> 
> > &nbsp;&nbsp;Subject: Re: Simple question  
> > &nbsp;&nbsp;Date: Mon 05 Nov 12 10:52:15PM +0900
> > 
> > Quoting August Y. (lists@ruby-forum.com):
> 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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](https://github.com/stomar/&gt);
