views:

63

answers:

2

I want to convert the string

"Full Time"

to

"full_time"

When I using "Full Time".underscore in irb, it prompt a error

NoMethodError: undefined method `underscore' for "Full Time":String

How can I fix it? Or is there any other way for me to get the above underscored result?

A: 

You could use gsub and replace all whitespaces to underscore

irb(main):008:0> "Full Time".downcase.gsub(/\s+/,"_")
=> "full_time"
S.Mark
it works well. thanks buddy. :)
siulamvictor
+4  A: 

There is a rails helper method called underscore. If you fire your rails console (script/console) you will be able to use it:

"FullTime".gsub(/\s+/,'').underscore.to_sym
:full_time 

so what i think that you should do is, remove space and then apply the method described above. Note that I added to_sym just to show that it is possible as well, but if you don't need it, just remove it.

Note that it is a rails helper and not a ruby method from the String class. It will just works in the rails enviornment.

VP
oooo i forgot this is a rails helper but i tested it in irb -_-||thanks for reminding me buddy. :)
siulamvictor