So I have a class Ball. in Ball we have a method type. What I want to do is return a string of the type of ball. The tricky part: if ball has no argument, I want to return string "standard". This handles the no argument case just fine. However, the "football" case keeps throwing and ArgumentError 1 for 0 error. What I'm trying to do is set a default of "standard" for if there is no argument passed to type and to print a given argument (given it's a string). How do I fix the ArgumentError? I've tried using splat and just taking 0 arguments already. Neither worked
class Ball
def type(ball="standard")
ball
end
end
Test.assert_equals Ball.new("football").ball_type, "football"
Test.assert_equals Ball.new.ball_type, "standard"
Since you're calling
new
onBall
, you should rename thetype
method toinitialize
. This method will automatically be called when a new instance ofBall
is constructed.@ball = ball
means that theball
argument is saved to the@ball
instance variable.It also looks like you want a method for accessing the ball type when you call
Ball.new.ball_type
:This method simply returns the value of the
@ball
instance variable, which was set in theinitialize
method.After these modifications: