Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Patrick Shushereba
Patrick Shushereba
10,911 Points

Class Not working

I'm working on this program in Code Wars. I'm supposed to write a class "Ball" that takes in an argument for "Ball Type". If Ball Type is empty, it's supposed to return "regular", otherwise it returns "super".

class Ball
  def initialize(ball_type)
    @ball_type = ball_type
  end

  def ball_type(ball_type)
      if ball_type.nil?
      return "regular"
    else
      return "super"
    end
  end
end

ball1 = Ball.new "super"
puts ball1

When I test this locally, it returns the memory location, but it's not printing out anything. And it's not passing the tests on the Code Wars site. Can anyone point me in the right direction so I can find a solution?

1 Answer

Mo Z
Mo Z
2,911 Points
class Ball
  def initialize (ball_type = nil)
    @ball_type = ball_type
  end

  def ball_type
    if @ball_type.nil?
      return "regular"
    else
      return "super"
    end
  end
end

ball1 = Ball.new("super").ball_type
ball2 = Ball.new.ball_type

puts ball1
puts ball2
Patrick Shushereba
Patrick Shushereba
10,911 Points

Thank you! I was making changes, and ended up having something closer to what you have here. I was setting the default value to "regular" instead of nil. I appreciate the help.