Wanting to capitalize the movie titles?


#1

https://www.codecademy.com/en/courses/ruby-beginner-en-Qn7Qw/2/5?curriculum_id=5059f8619189a5000201fbcb

I learn best when I apply the knowledge into what I do, so I tried to apply past concepts taught into this exercise that simply asks us to write code that prints the key of the given hash.

So what I'm trying to achieve is to put the name of the movie with the first word capitalized and the "_" replaced with a space (" "). Like how a proper movie title should look when written.

This is the error code that i'm getting:

undefined method `include?' for :memento:Symbol

Below is my code:

movie_ratings = {
  memento: 3,
  primer: 3.5,
  the_matrix: 3,
  truman_show: 4,
  red_dawn: 1.5,
  skyfall: 4,
  alex_cross: 2,
  uhf: 1,
  lion_king: 3.5
}

movie_ratings.each_key { |movie| 
    if movie.include? "_"
        movie.gsub!(/_/, " ")
    else
        movie
    end
    puts movie.capitalize, " " }

Tear the code apart if you have to, I'm trying to learn as much as I can. Thanks all. :smile:

Also, I did manage to complete the exercise, just trying some add-ons.


#2

You're on the right track and are doing a good job! Just one problem, so I'll give you a hint:

Hint: As you can see from the error message, the keys in the movies hash are actually symbols, and strings (not symbols) have the .include? method :wink:

Do you now know what to do?


#3

Yes I do! Thank you so much!! It works now. :smile:

Next question: By keeping my keys as symbols, how would I go around to make the second word (or third, or fourth) also capitalized? (there has to be another way other than changing the format of the symbols right?)

Current code:

movie_ratings.each_key { |movie| 
    movie = movie.to_s
    if movie.include? "_"
        movie.gsub!(/_/, " ")
    else
        movie
    end
    puts movie.capitalize, " " }

Your hint helped me a bunch by the way :smile:


#4

Symbols do not support such manipulation as capitalization. Even the official Symbol#capitalize method is the same as sym.to_s.capitalize.intern. So, you've got one step right - conversion of Symbol to String. Now, I would recommend you change your code like this:

# DESCRIPTION:
# takes a string with lower case words
# separated by an underscore, and returns a string
# with capitalized words separated by a space
def capitalizer(word)
    # magic
end

movie_ratings.each_key { |movie|     
    puts capitalizer(movie), " "  # line A
}

As you can see, writing the function separately will help you keep your code clean as there's gonna be a lot of statements. Now, there are two things for you to solve:

  1. I purposely left line A wrong. There are some things missing there, read the DESCRIPTION carefully and figure out what's missing.
  2. Fill in the capitalizer method.

Click to reveal spoiler - AT YOUR OWN RISK! :wink:
Hint for point 1: It is related to String<-->Symbol conversion
Hint for point 2: You will need to use a while loop and manipulate individual words (use .split)


#5

Thanks so much again. :slight_smile: I had to take a peak at the hints you gave after an hour of trying things. Oh well.

However, I couldn't think of a way to create the while loop you suggested or change the array from split to strings to capitalize. Here's what I have:

def capitalizer(words) 
    if 
        title = words.split("_") 
        title.each { |word| puts word.capitalize, " " }

    else
        words.capitalize
    end
end

movie_ratings.each_key { |movie| 
    capitalizer(movie.to_s) }

This code does capitalize everything but the output looks like this:

Thanks for being so patient and helping me. It really does mean a lot. :slight_smile:


#6

There are always more than one kind of solutions to a problem and I'm glad you figured out a new one! :smiley:

I find two strange things with your code though :confused:

  1. On the second line, you are missing the if condition?
  2. You are not printing the result else branch?

You are using puts instead of print which is causing your output to be on the different line.