views:

449

answers:

1

Eclipse (RedRails) complain about "Feature envy" in the following code:

if input_text =~ /^(---\s*\n.*?\n?)(---.*?)/m
  content_text = input_text[($1.size + $2.size)..-1] # warning in $1

  header = YAML.load($1)

  @content = content_text.strip()
  @title = header["title"]
end

My understanding is that I safe to ignore this warning. But I am wander why this warning generated. I cannot understand how I can extract method for $1.size and $1. Pls, explain.

+2  A: 

Reek is telling you that, because you are adding two properties of the same class, the calculation should actually belong in String. When adding string lengths this is nonsense of course, but in your case the code can be simplified by using $& (the complete matched string):

input_text[$&.size..-1]
molf
Not strictly true. Firstly, Reek doesn't report FeatureEnvy for the code as shown above. And secondly, $1 and $2 are different objects, so Reek won't suggest that this code could be moved onto either.
kevinrutherford