Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 611 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ruby gsub multiple characters in string

#1
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.
Reply

#2
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:

[To see links please register here]


gg
Reply

#3
[`String#gsub`](

[To see links please register here]

) and [`Hash#fetch`](

[To see links please register here]

) 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"
Reply

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

#5
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"
Reply

#6
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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through