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

Ruby Case Statement Operator

When I run the program and I type in one of the players included in the Case Statement it doesn't accept. The program just ends.

def player 
  print "Who's Your Favorite NBA Player of All Time?: "
  player = gets.chomp.downcase 

  loop do
    if player.length >= 3 && !player.index(/\d/)
      break
    else
      puts "Player Name must be longer than 3 characters and not contain numbers."
    end
  end
end

case player
    when "allen iverson"
        puts "Bubbachuck aka The Answer, yes, he was a beast.  Nice choice."
    when "larry bird"
        puts "Nice one.  2nd greatest small forward ever.  Behind you know who... The King!"
end

2 Answers

Nathan Williams
seal-mask
.a{fill-rule:evenodd;}techdegree
Nathan Williams
Python Web Development Techdegree Student 6,851 Points

Hi Daniel,

Super close, but you're getting caught by two things:

1) your variable name and your method name are the same. since ruby lacks sigils, both player the variable and player the method are referenced with the same expression. you should generally avoid conflicting names like this

2) you're getting caught by the variable "scope", which is basically "the section of code within which a variable is known once defined". in general, this is "within the current code block". so, when you define the player variable within your method definition, the variable is only accessible within that method. you can work around this (and make your methods reusable) by passing messages between methods as arguments.

here's a similar example, correcting for the conflicting variable/method name and passing the user's input into the response method as an argument so we don't have to worry about the variable scope.

def get_player 
  print "Who's Your Favorite NBA Player of All Time?: "

  loop do
    player = gets.chomp.downcase
    if player.length >= 3 && !player.index(/\d/)
      respond(player)
      break
    else
      puts "Player Name must be longer than 3 characters and not contain numbers."
    end
  end
end

def respond(player = nil)
  case player
  when "allen iverson"
    puts "Bubbachuck aka The Answer, yes, he was a beast.  Nice choice."
  when "larry bird"
    puts "Nice one.  2nd greatest small forward ever.  Behind you know who... The King!"
  else
    puts 'unknown player'
  end
end

get_player

Hope that helps! Feel free to ask for clarification if something's not clear :)

Ok thanks for this! A couple questions.

  1. Why do you have pass in (player = nil) into the respond method?

  2. If a user enters in a name that isn't listed in the respond method, how could I then prompt the user to try again and have my program cycle back through the list of names?

Nathan Williams
seal-mask
.a{fill-rule:evenodd;}techdegree
Nathan Williams
Python Web Development Techdegree Student 6,851 Points

Hey Daniel,

Why do you have pass in (player = nil) into the respond method?

When you specify arguments when defining the function, you're actually naming the variable you'll reference the argument as within the method. We've got a video that describes method arguments in more detail here

how could I then prompt the user to try again and have my program cycle back through the list of names?

In the case statement, you'd want to use the 'else' section to catch unmet conditions. In this case, you could just puts a string explaining that you don't know their named player and then call the get_player method again to re-prompt them for another play name

Cheers!

Ok got it. Yeah, I'm still not sure that I'm clear on why I'd need to add the "= nil" part of it. I ran the code below and it worked exactly the same as it did with out "=nil"

def respond(player)
  case player
  when "allen iverson"
    puts "Bubbachuck aka The Answer, yes, he was a beast.  Nice choice."
  when "larry bird"
    puts "Nice one.  2nd greatest small forward ever.  Behind you know who... The King!"
  else
    puts 'unknown player'
    get_player
  end
end

get_player