Q:

Get the number of gsub substitutions in Ruby

gsub returns the string, or nil. Is there a way to have it return the number of replacements it has made?

A:

I can think of using gsub block like this:
count = 0
str.gsub(/pat/) { |a| count+=1; "replacement" }

Example:
str = "lets replace all s with S"
count = 0
str.gsub("s") { |a| count+=1; "S" }
count
#=> 2

A:

In Ruby gsub without second argument returns an Enumerator and you can use it:
str = 'your string'
new_str = str.gsub(/your/, 'my')
count = str.gsub(/your/).count

