From: Lloyd Linklater Date: 2008-09-15T22:18:51+09:00 Subject: Re: Need someone who can help 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-forum.com/.