Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby method to make a string like 'Hello World' be like 'hello_world'

Tags:

ruby

What is the Ruby method to make a string like Hello World be like hello_world.

like image 279
Nerian Avatar asked Apr 19 '11 16:04

Nerian


People also ask

How do you Downcase a string in Ruby?

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.

What does the strip method do in Ruby?

The . strip method removes the leading and trailing whitespace on strings, including tabs, newlines, and carriage returns ( \t , \n , \r ).

How do you return a string in Ruby?

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 .

Can you use each on a string Ruby?

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.


1 Answers

You don't need Rails helpers. You can do it in pure ruby.

'Hello World'.downcase.tr(' ', '_')
# => hello_world
like image 71
Simone Carletti Avatar answered Sep 23 '22 12:09

Simone Carletti