Right now I have this code: @category.name.gsub(' ', '-').gsub('--','-').gsub('--','-')
What id does: If I have category with name sometext sometext
, it will change all space charactes to -
dash characters. sometext-someteext
( I use this for url building)
.gsub('--','-').gsub('--','-')
- this part I need for the case when name is something like sometext - sometext
so without this part my method will give me wrong output like sometext---sometext
So what is a more elegant way to rewrite that 3 gsubs into one?
正则表达式解救:
.gsub(/ \-+ /, ' - ')
ActiveSupport::Inflector contains parameterize which is a more general for building urls.
> 'sometext - sometext'.parameterize
=> "sometext-sometext"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.