Setting Up the 'If' Branch, Part 1


#1

I'm not stuck but curious about certain syntax for this exercise. When I did (1) & (3), it works. But when I tried (2), I had an error. Any explanations?

https://www.codecademy.com/en/courses/ruby-beginner-en-JdNDe/0/4?curriculum_id=5059f8619189a5000201fbcb

(1)

user_input = gets.chomp
user_input.downcase!

(2)

user_input = gets.chomp.downcase!

(3)

user_input = gets.chomp.downcase

#2

.downcase! works fine for me (Chrome Version 58.0.3029.110 (64-bit))

Which browser are you using?


#3

I use chrome. when using the syntax:

user_input = gets.chomp
user_input.downcase!

works fine but when using:

user_input = gets.chomp.downcase!

acted goofy and kept getting an error. Then tried:

user_input = gets.chomp.downcase

and it worked. No clue why lol


#4

.downcase! will modify the string in place, whereas .downcase will return a new string.

If your string is already in lowercase, it will return nil – and you'll get the error you're getting.

e.g.
- If your user input is: this is a string, you'll get a nil return error (as the entire string is already lowercase)
- If your user input is: This is a string (or THIS IS A STRING), it will work.

And I think this works regardless...

user_input = gets.chomp
user_input.downcase!

... because the user input has already been assigned to variable user_input, and then .downcase! is applied to it.

Hope that answers your question?


#5

Yea, I believe it does. Thank you!


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.