views:

1790

answers:

3

Hey, I'm writing my first Rails app, and I'm trying to replace the underscores form an incoming id name with spaces, like this:

before: test_string

after: test string

How can I do this? Sorry if this is a bit of a dumb question, I'm not very familiar with regular expressions...

+2  A: 

You can use the following:

  /_/ /g

This tells the regular expression parser to search for an underscore (/_) and replace with a space (/ ) all occurrences (/g).

Dave Jarvis
+3  A: 
str.gsub!(/_/, ' ')

gsub stands for 'global substitution', and the exclamation means it'll change the string itself rather than just return the substituted string.

You can also do it without regexes using String#tr!:

str.tr!('_', ' ')
yjerem
A: 

Whoops, I actually had it working--just forgot to update the variable name :P

I was using this:

@id = params[:id]
@title = @id.gsub("_", " ")
mportiz08