how to safely replace all whitespaces with underscores with ruby?

Ruby on-RailsRubyRuby on-Rails-3

Ruby on-Rails Problem Overview


This works for any strings that have whitespaces in them

str.downcase.tr!(" ", "_")

but strings that dont have whitespaces just get deleted

So "New School" would change into "new_school" but "color" would be "", nothing!

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Pass '_' as parameter to parameterize(separator: '-'). For Rails 4 and below, use str.parameterize('_')

Examples:

> with space

str = "New School"
str.parameterize(separator: '_')

=> "new_school"

> without space

str = "school"
str.parameterize(separator: '_')

=> "school"

You can also solve this by chaining underscore to parameterize.

Examples:

> with space

str = "New School"
str.parameterize.underscore

=> "new_school"

> without space

str = "school"
str.parameterize.underscore

=> "school"

Solution 2 - Ruby on-Rails

The docs for tr! say

> Translates str in place, using the same rules as String#tr. Returns str, or nil if no changes were made.

I think you'll get the correct results if you use tr without the exclamation.

Solution 3 - Ruby on-Rails

If you're interested in getting a string in snake case, then the proposed solution doesn't quite work, because you may get concatenated underscores and starting/trailing underscores.

For example

1.9.3-p0 :010 > str= "  John   Smith Beer "
  => "  John   Smith Beer " 
1.9.3-p0 :011 > str.downcase.tr(" ", "_")
  => "__john___smith_beer_"

This solution below would work better:

1.9.3-p0 :010 > str= "  John   Smith Beer "
  => "  John   Smith Beer " 
1.9.3-p0 :012 > str.squish.downcase.tr(" ","_")
  => "john_smith_beer" 

squish is a String method provided by Rails

Solution 4 - Ruby on-Rails

If you are using rails 5 and above you can achieve the same thing with

str.parameterize(separator: '_')

Solution 5 - Ruby on-Rails

Old question, but...

For all whitespace you probably want something more like this:

"hey\t there   world".gsub(/\s+/, '_') # hey_there_world

This gets tabs and new lines as well as spaces and replaces with a single _.

The regex can be modified to suit your needs. E.g:

"hey\t there   world".gsub(/\s/, '_') # hey__there___world

Solution 6 - Ruby on-Rails

str.downcase.tr(" ", "_")

Note: No "!"

Solution 7 - Ruby on-Rails

str = "Foo Bar"
str.tr(' ','').underscore

=> "foo_bar"

Solution 8 - Ruby on-Rails

You can also do str.gsub(" ", "_")

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrugbertView Question on Stackoverflow
Solution 1 - Ruby on-RailsSampat BadheView Answer on Stackoverflow
Solution 2 - Ruby on-RailsrwilliamsView Answer on Stackoverflow
Solution 3 - Ruby on-RailsZack XuView Answer on Stackoverflow
Solution 4 - Ruby on-RailstheterminalguyView Answer on Stackoverflow
Solution 5 - Ruby on-Railsbr3ntView Answer on Stackoverflow
Solution 6 - Ruby on-RailsMichael DurrantView Answer on Stackoverflow
Solution 7 - Ruby on-RailsTripView Answer on Stackoverflow
Solution 8 - Ruby on-RailsrandomuserView Answer on Stackoverflow