[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Need someone who can help

Dot Baiki

9/15/2008 1:11:00 PM

Hello

Please, can someone help me with this problem. I am really stuck. Can't
find my problem. Simple code snippet is not working at all. I totally
desperate.

Look at this:
--------------------start
#!/usr/bin/env ruby

class AreaOfCircle

PI = 3.14159265

def initialize(radius, area)
@radius = radius
@area = area
end

def calc_area(radius)
@area = PI * @radius**2
end
end

print 'Enter radius of circle: '
keyboard_input = gets
radius = keyboard_input.to_f

instance = AreaOfCircle.new
screen_output = instance.calc_area(keyboard_input)
puts screen_output
----------------------------------- end

I'm already pulling my hairs (not much left by now). Hopefully you can
give me any hint!

Regards,
Baiki
--
Posted via http://www.ruby-....

10 Answers

Nathan Powell

9/15/2008 1:17:00 PM

0

Are you looking at the error? It clearly tells you what the problem is.

"wrong number of arguments". Your initialize method takes two
parameters, and you aren't passing in any.

On Mon, Sep 15, 2008 at 10:11:28PM +0900, Dot Baiki wrote:
> Hello
>
> Please, can someone help me with this problem. I am really stuck. Can't
> find my problem. Simple code snippet is not working at all. I totally
> desperate.
>
> Look at this:
> --------------------start
> #!/usr/bin/env ruby
>
> class AreaOfCircle
>
> PI = 3.14159265
>
> def initialize(radius, area)
> @radius = radius
> @area = area
> end
>
> def calc_area(radius)
> @area = PI * @radius**2
> end
> end
>
> print 'Enter radius of circle: '
> keyboard_input = gets
> radius = keyboard_input.to_f
>
> instance = AreaOfCircle.new
> screen_output = instance.calc_area(keyboard_input)
> puts screen_output
> ----------------------------------- end
>
> I'm already pulling my hairs (not much left by now). Hopefully you can
> give me any hint!
>
> Regards,
> Baiki
> --
> Posted via http://www.ruby-....
>

--
nathan
nathan_at_nathanpowell_dot_org

What kind of crazy nut would spend two or three hours a day just running?
~ Steve Prefontaine
------------------------------------

Lloyd Linklater

9/15/2008 1:19:00 PM

0

Dot Baiki wrote:
> Hello
>
> Please, can someone help me with this problem. I am really stuck. Can't
> find my problem. Simple code snippet is not working at all. I totally
> desperate.
>
> Look at this:
> --------------------start
> #!/usr/bin/env ruby
>
> class AreaOfCircle
>
> PI = 3.14159265
>
> def initialize(radius, area)
> @radius = radius
> @area = area
> end
>
> def calc_area(radius)
> @area = PI * @radius**2
> end
> end
>
> print 'Enter radius of circle: '
> keyboard_input = gets
> radius = keyboard_input.to_f
>
> instance = AreaOfCircle.new
> screen_output = instance.calc_area(keyboard_input)
> puts screen_output
> ----------------------------------- end
>
> I'm already pulling my hairs (not much left by now). Hopefully you can
> give me any hint!
>
> Regards,
> Baiki

Well, I can see two things at a glance.

1. You pass in the string rather than the radius which is to_f.
2. The initialization requires TWO arguments.

Try something like this:

class AreaOfCircle

PI = 3.14159265

def initialize(radius, area)
@radius = radius
@area = area
end

def calc_area(radius)
@area = PI * @radius**2
end
end


radius = 4.0

instance = AreaOfCircle.new(4, 3)
screen_output = instance.calc_area(radius)
puts screen_output
--
Posted via http://www.ruby-....

Reid Thompson

9/15/2008 1:38:00 PM

0

On Mon, 2008-09-15 at 22:11 +0900, Dot Baiki wrote:
> Hello
>
> Please, can someone help me with this problem. I am really stuck. Can't
> find my problem. Simple code snippet is not working at all. I totally
> desperate.
>
> Look at this:
> --------------------start
> #!/usr/bin/env ruby
>
> class AreaOfCircle
>
> PI = 3.14159265
>
> def initialize(radius, area)
> @radius = radius
> @area = area
> end
>
> def calc_area(radius)
> @area = PI * @radius**2
> end
> end
>
> print 'Enter radius of circle: '
> keyboard_input = gets
> radius = keyboard_input.to_f
>
> instance = AreaOfCircle.new
> screen_output = instance.calc_area(keyboard_input)
> puts screen_output
> ----------------------------------- end
>
> I'm already pulling my hairs (not much left by now). Hopefully you can
> give me any hint!
>
> Regards,
> Baiki#!/usr/bin/env ruby

class AreaOfCircle

PI = 3.14159265

def initialize(radius)
@radius = radius
@area = nil
end

def calc_area(radius)
@area = PI * @radius**2
end
end

print 'Enter radius of circle: '
keyboard_input = gets
radius = keyboard_input.to_f

instance = AreaOfCircle.new radius
screen_output = instance.calc_area(radius)
puts screen_output

Dot Baiki

9/15/2008 2:08:00 PM

0

Hey! Amazing quick response! Outstanding. Sweet solutions! Will continue
reading my books I bought. Really cool. Thanks so much. This is what I
made now:

--- begin ---
#!/usr/bin/env ruby

class AreaOfCircle

PI = 3.14159265

def initialize(radius)
@radius = radius
@area = nil
end

def calc_area(radius)
@area = PI * @radius**2
end
end

print 'Enter radius of circle: '
radius = gets.to_f
puts "You entered: #{radius}"

instance = AreaOfCircle.new(radius)
screen_output = instance.calc_area(radius)
puts "Result: #{screen_output}"
--- end ---

You know, the cool thing is that I really soooooon will understand Ruby
and its beauty :-)

Baiki
--
Posted via http://www.ruby-....

Lloyd Linklater

9/15/2008 2:18:00 PM

0

Dot Baiki wrote:
> Hey! Amazing quick response! Outstanding. Sweet solutions! Will continue
> reading my books I bought. Really cool. Thanks so much. This is what I
> made now:
>
> --- begin ---
> #!/usr/bin/env ruby
>
> class AreaOfCircle
>
> PI = 3.14159265
>
> def initialize(radius)
> @radius = radius
> @area = nil
> end
>
> def calc_area(radius)
> @area = PI * @radius**2
> end
> end
>
> print 'Enter radius of circle: '
> radius = gets.to_f
> puts "You entered: #{radius}"
>
> instance = AreaOfCircle.new(radius)
> screen_output = instance.calc_area(radius)
> puts "Result: #{screen_output}"
> --- end ---
>
> You know, the cool thing is that I really soooooon will understand Ruby
> and its beauty :-)
>
> Baiki

I am glad that it helped, though it seems that you have more than you
strictly seem to need. e.g.

class AreaOfCircle

PI = 3.14159265

def calc_area(radius)
PI * radius**2
end
end

is plenty. Note: no parameter needed for the AreaOfCircle.new
--
Posted via http://www.ruby-....

Dot Baiki

9/15/2008 2:46:00 PM

0

Lloyd Linklater wrote:
> I am glad that it helped, though it seems that you have more than you
> strictly seem to need. e.g.
>
> class AreaOfCircle
>
> PI = 3.14159265
>
> def calc_area(radius)
> PI * radius**2
> end
> end
>
> is plenty. Note: no parameter needed for the AreaOfCircle.new

I totally agree. I just thought it's better to allocate this memory in
order to write "clean" code. I just started with Ruby a few days ago,
hence I'm a total beginner (also with the English language).

If I don't do this "initialize" thing: no drawbacks?

Baiki
--
Posted via http://www.ruby-....

Lloyd Linklater

9/15/2008 3:19:00 PM

0

Dot Baiki wrote:
> Lloyd Linklater wrote:
>> I am glad that it helped, though it seems that you have more than you
>> strictly seem to need. e.g.
>>
>> class AreaOfCircle
>>
>> PI = 3.14159265
>>
>> def calc_area(radius)
>> PI * radius**2
>> end
>> end
>>
>> is plenty. Note: no parameter needed for the AreaOfCircle.new
>
> I totally agree. I just thought it's better to allocate this memory in
> order to write "clean" code. I just started with Ruby a few days ago,
> hence I'm a total beginner (also with the English language).
>
> If I don't do this "initialize" thing: no drawbacks?
>
> Baiki

There should be no drawbacks. In fact, the radius is not known until
the AreaOfCircle method is called so, in this case, initializing it is
not required. The Area variable is not really used as far as we can
see. You did put this into a class so it may be that it is used
elsewhere. As far as memory allocation, it is not necessary. Ruby does
all that automatically and needs no help from us.
--
Posted via http://www.ruby-....

Doug Glidden

9/15/2008 3:39:00 PM

0

Dot Baiki wrote:
> Hey! Amazing quick response! Outstanding. Sweet solutions! Will continue
> reading my books I bought. Really cool. Thanks so much. This is what I
> made now:
>
> --- begin ---
> #!/usr/bin/env ruby
>
> class AreaOfCircle
>
> PI = 3.14159265
>
> def initialize(radius)
> @radius = radius
> @area = nil
> end
>
> def calc_area(radius)
> @area = PI * @radius**2
> end
> end
>
> print 'Enter radius of circle: '
> radius = gets.to_f
> puts "You entered: #{radius}"
>
> instance = AreaOfCircle.new(radius)
> screen_output = instance.calc_area(radius)
> puts "Result: #{screen_output}"
> --- end ---
>
> You know, the cool thing is that I really soooooon will understand Ruby
> and its beauty :-)
>
> Baiki

Baiki,

I think you might end up getting surprised by what happens when you do
this. For instance, consider the results of the following code, given
your existing AreaOfCircle class (of which I really dislike the name,
but never mind that for now):

circle = AreaOfCircle.new 5 # circle has @radius=5, @area=nil
area = circle.calc_area 3 # assigns @area=78.540... (25*PI), i.e.
the radius
# parameter is completely ignored

So get rid of the completely unnecessary parameter for calc_area.
Furthermore, the user probably doesn't want to have to remember to
manually call for the area to be calculated when he creates a circle, so
just do that in your constructor. Add accessors for @radius and @area
and you're really cookin'. It might look something like this:

class MyCircle

attr_accessor :radius, :area

PI = 3.14159265

def initialize(radius)
@radius = radius
calc_area
end

def calc_area
@area = PI * @radius**2
end
end

Your program code could then look like this:

print 'Enter radius of circle: '
radius = gets.to_f
puts "You entered: #{radius}"

instance = MyCircle.new(radius)
screen_output = instance.area
puts "Result: #{screen_output}"

On the other hand, if all you're interested in is calculating the area
and you don't actually need to store the information, use a mixin
instead of a full-blown class:

Module AreaOfCircle
PI = 3.14159265

def calc_area(radius)
PI * radius**2
end
end

# begin program code

include AreaOfCircle

print 'Enter radius of circle: '
radius = gets.to_f
puts "You entered: #{radius}"

screen_output = calc_area radius
puts "Result: #{screen_output}"

I know I threw a lot of stuff at you, so feel free to ask about anything
you don't understand there.

Doug
--
Posted via http://www.ruby-....

Dot Baiki

9/15/2008 3:49:00 PM

0

> There should be no drawbacks. In fact, the radius is not known until
> the AreaOfCircle method is called so, in this case, initializing it is
> not required. The Area variable is not really used as far as we can
> see. You did put this into a class so it may be that it is used
> elsewhere. As far as memory allocation, it is not necessary. Ruby does
> all that automatically and needs no help from us.

Great, I'll take this into account. Thanks a lot, and hope I can count
on you with my next problem (which will come for sure).

Baiki
--
Posted via http://www.ruby-....

Dot Baiki

9/15/2008 4:40:00 PM

0

Doug Glidden wrote:
> On the other hand, if all you're interested in is calculating the area
> and you don't actually need to store the information, use a mixin
> instead of a full-blown class:
>
> Module AreaOfCircle
> PI = 3.14159265
>
> def calc_area(radius)
> PI * radius**2
> end
> end
>
> # begin program code
>
> include AreaOfCircle
>
> print 'Enter radius of circle: '
> radius = gets.to_f
> puts "You entered: #{radius}"
>
> screen_output = calc_area radius
> puts "Result: #{screen_output}"
>
> I know I threw a lot of stuff at you, so feel free to ask about anything
> you don't understand there.
>
> Doug

Hi Doug,

Uffff :-) Need more time to analyze your code. Need also to read next
chapter in my book first. Take a little time. I'm not genius. But, great
input.

Baiki
--
Posted via http://www.ruby-....