value = "some value" value.sub!(/\A(\S*)(.*?)(\S*)\Z/m) do |m| <<'EOT' #$1 other #$3 EOT end
This should produce
some other value\n
but it does not contain the newline value.