7 - update must be in lowercase


#1

Everyone - please make sure you type "update" in lowercase. I used .upcase! on my gets.chomp and UPDATE as my case, and the code would not go through. Just


#2

If you want to use both "update" and "UPDATE" as commands, you just have to downcase or upcase the input.

but you can't do it like this:

input = gets.chomp.upcase!

You have to do it either like this:

input = gets.chomp.upcase

or like this:

input = gets.chomp
input.upcase!

#3

Why can't we do gets.chomp.upcase! ?


#4

When you do

input = gets.chomp.upcase

If you type "yolo" it goes like this:

  • user sends "yolo" to gets
  • gets returns "yolo" (with the newline character) to chomp
  • chomp returns "yolo" (without the newline character) to upcase
  • upcase returns "YOLO"
  • Ruby then reads the operator = and assigns "YOLO" to input

input has "YOLO", everything worked out.


However, if you use:

input = gets.chomp.upcase!

It goes like this:

  • user sends "yolo" to gets
  • gets returns "yolo" (with the newline character) to chomp
  • chomp returns "yolo" (without the newline character) to upcase!
  • upcase! does NOT return "YOLO", it's not supposed to, so it just modifies the object correctly and leave it as it is, and instead it will return nil.
  • Ruby then reads the operator = and assigns nil to input

Nothing worked correctly because input now has nil as a value, and that's not what we wanted.


#5

That was very helpful! Thank you!


#6

As a side note:

After input has a string stored, input.upcase! will work correctly of course, because even if upcase! didn't return anything, we still have the object under the name input at our disposal.