Question

I created a simple program to ask a sports fan about some info. Here is the code I have so far:

puts "What's your favorite pro sport?"
favorite_sport = gets.chomp

puts "Who's your favorite team in the #{favorite_sport}?"
favorite_team = gets.chomp

puts "What city are they from?"
team_city = gets.chomp

puts "Who's your favorite player in the #{favorite_team} roster?"
favorite_player = gets.chomp

puts "What position does #{favorite_player} play?"
player_position = gets.chomp

puts "How many years has #{favorite_player} played in the #{favorite_sport}"
years_experience = gets.chomp

fan_info = [favorite_sport, favorite_team, team_city, favorite_player, player_position, years_experience]
puts fan_info

I want to have the program output the fan_info with the first letter of the string capitalized. How do I do this? I know I have to use the method capitalize but I am having trouble implementing this.

Here is an example of the input and output:

What's your favorite pro sport?
NFL
Who's your favorite team in the NFL?
Seahawks
What city are they from?
seattle
Who's your favorite player in the Seahawks roster?
wilson
What position does wilson play?
qb
How many years has wilson played in the NFL
1
NFL
Seahawks
seattle
wilson
qb
1
Was it helpful?

Solution 2

If your intention is to capitalize the first letter keeping the other letters intact (i.e., "NFL" stays to be "NFL", and does not become "Nfl"), then do:

favorite_sport = gets.chomp.sub(/./, &:upcase)
...

OTHER TIPS

Try this:

puts fan_info.map(&:capitalize)

This calls #capitalize on every string, builds a new array of all the results, and prints that out instead.

It's equivalent to something like this:

fan_info_capitalized = []
fan_info.each do |inf|
  fan_info_capitalized << inf.capitalize
end
puts fan_info_capitalized

Only much more compact.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top