rails: String#underscore doesn't convert spaces to underscores
Why Rails String#underscore doesn’t convert spaces to underscores? The same is for String#camelize why it doesn’t convert spaces to nothing? Is there something behind it or I am missing the point?
1.9.3-p0 :001 > "Hello World".underscore
=> "hello world"
1.9.3-p0 :002 > "HelloWorld".underscore
=> "hello_world"
1.9.3-p0 :004 > "hello_world".camelize
=> "HelloWorld"
1.9.3-p0 :005 > "hello world".camelize
=> "Hello world"
About this issue
- Original URL
- State: closed
- Created 12 years ago
- Reactions: 10
- Comments: 26 (18 by maintainers)
@fxn I think this was for me. arrgh!! I can use it now like this and it solves my problem.
.parameterize.underscore
works, but you can also do.parameterize('_')
.parameterize('_')
is now deprecated, and we should use.parameterize(separator: '_')
.@pokonski do you know
parameterize
? http://guides.rubyonrails.org/active_support_core_extensions.html#parameterizeThat’s sad because if method says
underscore
it should underscore, it’s also true forcamelize
"CamelCase".parameterize
returns'camelcase'
, which might not be the desired result..underscore.parameterize('_')
seems to work fine for many strings:['Hello World', 'Hello-World', 'HelloWorld', 'Hello/World', 'helloWorld', 'hello_world', 'hëllö+Wörld'].map{|s| s.underscore.parameterize('_')}.uniq #=> ["hello_world"]
@shtirlic awesome!