What is the Ruby method to make a string like Hello World
be like hello_world
.
In Ruby, we use the downcase method to convert a string to lowercase. This method takes no parameters and returns the lowercase of the string that calls it. It returns the original string if no changes are made. Note: This method does not affect the original string.
The . strip method removes the leading and trailing whitespace on strings, including tabs, newlines, and carriage returns ( \t , \n , \r ).
In Ruby functions, when a return value is not explicitly defined, a function will return the last statement it evaluates. If only a print statement is evaluated, the function will return nil . The above will print the string printing , but return the string returning .
Iterate Over Characters Of a String in Ruby You can also use the chars method to convert the string into an array of characters. Then you can use each on this array to iterate.
You don't need Rails helpers. You can do it in pure ruby.
'Hello World'.downcase.tr(' ', '_')
# => hello_world
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With