Reputation: 15161
I want to replace string in CSV::Table
.
I could replace the string by using gsub!
like this:
csv = CSV.table(@csv_file)
csv[:tag].each do |tag|
tag.gsub!('Replace1','Replace2')
tag.gsub!('Replace3','Replace4')
end
But I prefer to use gsub
with method chain
csv[:tag].each do |tag|
tag = tag.gsub('Replace1','Replace2').
gsub('Replace3','Replace4')
end
Unfortunately it doesn't change the csv[:tag] strings.
How can I replace string in CSV::Table class without using gsub!
?
Upvotes: 0
Views: 52
Reputation: 198334
gsub!
returns self
, so you can do the same thing:
tag.gsub!('Replace1','Replace2').
gsub!('Replace3','Replace4')
If you want to replace a string with a string you already calculated, you can use String#replace
:
new_tag = tag.gsub('Replace1','Replace2').
gsub('Replace3','Replace4')
tag.replace(new_tag)
Upvotes: 1