Mega Code Archive

 
Categories / Ruby / Development
 

The part of a string matched by a regular expression can be replaced

line = "this is a Perl. And this a Python" puts line line.sub(/Perl/, 'Ruby')    # replace first 'Perl' with 'Ruby'  puts line line.gsub(/Python/, 'Ruby') # replace every 'Python' with 'Ruby'  puts line