> string.scan(/[^\\]\$/).size
This fails on input like "$$$$", since the string will be split into "$
$", "$$". It will also not find the first $ in the string but this
could be amended by using (^|[^\\]).