Why do we use " ! " in capitalize method ?!


#1



Replace this line with your code.


#2

Without the !, the string isn't changed.

Test it out for yourself with this code.

my_string = "string"
my_string.capitalize
puts my_string
my_string.capitalize!
puts my_string

You can do this:

my_string_2 = "string2"
my_new_string = my_string_2.capitalize
puts my_new_string

#3

Thanks for your help , i will try it .


#4

string isn't capitalized in the first case ( capitalize) but it was capitalized when i wrote ( capitalize!).Thank you !


#6

so is this true?

var.upcase!

var = var.upcase

And why isn't the ! introduced in the first lesson when upcase is introduced? Would one ever use it without the !?


#7

Both of those will give you the same result. Whatever string is stored in var will be all uppercase.

A reason why you might not want to use ! is that you don't want to change your original value.


#8

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