简体   繁体   中英

Ruby gsub multiple characters in string

Using Ruby 1.9.3, Rails 3.2, I have the following:

"every good boy does fine".gsub("every", "all").gsub("boy", "girl").gsub("fine", "well")
# => "all good girl does well"

Is there a better way to write this? Thanks.

String#gsub and Hash#fetch will be the good choice for this.

a = "every good boy does fine"
h = {"every" => "all","boy" => "girl", "fine" =>"well" }
a.gsub(/\w+/) { |m| h.fetch(m,m)}
# => "all good girl does well"

or,

a = "every good boy does fine"
h = {"every" => "all","boy" => "girl", "fine" =>"well" }
Regexp.new("^#{h.keys.join('|')}$") # => /^every|boy|fine$/
a.gsub(Regexp.new("^#{h.keys.join('|')}$"),h)
# => "all good girl does well"
replacements = [ ["every", "all"], ["boy", "girl"],["fine", "well"]
replacements.each {|replacement| str.gsub!(replacement[0], replacement[1])}

I don't know if its better, but much cleaner.

subs = { "every" => "all", "boy" => "girl", "fine" => "well" }
"every good boy does fine".gsub(/\w+/) { |m| subs[m] || m }
# => 'all good girl does well'

just do this:

"every good boy does fine".gsub(/\\w+/, 'every' => 'all', 'boy' => 'girl', 'fine' => 'well')

a lot easier to read :) some of the answers here to this simple question really make me wonder...lol

here's the reference: http://ruby-doc.org/core-2.1.4/String.html

gg

If the intent is to avoid method chaining:

[9] pry(main)> "every good boy does fine".gsub("every", "all").gsub("boy", "girl").gsub("fine", "well")
=> "all good girl does well"
[10] pry(main)> "every good boy does fine".gsub(/(every)|(boy)|(fine)/) do |word|
[10] pry(main)*   case word
[10] pry(main)*   when "every"
[10] pry(main)*     "all"
[10] pry(main)*   when "boy"
[10] pry(main)*     "girl"
[10] pry(main)*   when "fine"
[10] pry(main)*     "well"
[10] pry(main)*   end
[10] pry(main)* end
=> "all good girl does well"

Alternatively:

[11] pry(main)> REPLACEMENT = { "every" => "all", "boy" => "girl", "fine" => "well"}
=> {"every"=>"all", "boy"=>"girl", "fine"=>"well"}
[12] pry(main)> "every good boy does fine".gsub(/(every)|(boy)|(fine)/) { |word| REPLACEMENT[word] }
=> "all good girl does well"

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM