Bang!
Explore the concept of bang methods in Ruby, how they modify objects directly, and the consequences of using them. Understand the difference between normal and bang methods through practical examples, and gain insight into Ruby's object referencing and memory behavior to become a confident Ruby developer.
We'll cover the following...
Another exciting challenge we should tackle is what we call the “bang,” “exclamation mark,” or just the ! at the end of some methods. Let’s look at the Ruby program below. If we’re using non-English characters on Windows, we may encounter some difficulties with the downcase method:
Note: The text is
“I AM COOL,not“I’M COOL”—we can use that too, but we will need to keep it in double quotes because this phrase contains an apostrophe.
We define the variable with the value “I AM COOL” in all capital letters. On the second line, we redefine the variable and assign a new value to it, resulting in the x.downcase operation. Since x is a string, we have the right to call any method for the String class. We call the downcase method ...