Need someone who can help

D

Dot Baiki

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
 
N

Nathan Powell

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.

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

--
nathan
nathan_at_nathanpowell_dot_org

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

Lloyd Linklater

Dot said:
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
 
R

Reid.Thompson

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
 
D

Dot Baiki

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
 
L

Lloyd Linklater

Dot said:
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
 
D

Dot Baiki

Lloyd said:
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
 
L

Lloyd Linklater

Dot said:
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.
 
D

Doug Glidden

Dot said:
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
 
D

Dot 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.

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
 
D

Dot Baiki

Doug said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top