Need someone who can help

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-forum.com/.

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-forum.com/\.

--
nathan
nathan_at_nathanpowell_dot_org

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

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/\.

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

···

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

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 :slight_smile:

Baiki

···

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

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 :slight_smile:

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-forum.com/\.

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 :slight_smile:

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-forum.com/\.

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-forum.com/\.

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 :slight_smile: 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-forum.com/\.

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-forum.com/\.

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-forum.com/\.