You can refactor this a bit like this:

source[def_lineno..end_lineno].reject(&:empty?).size

It's more idiomatic is easier to read IMHO.