0

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 wandering why this warning is generated. I cannot understand how I can extract method for $1.size and $1.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Mike Chaliy
  • 25,801
  • 18
  • 67
  • 105
  • 1
    Can you post a bit more of the surrounding code? I had a go at feeding this into reek but couldn't get it to complain about Feature Envy - Thanks – mikej Jul 25 '09 at 15:24
  • 1
    Can you please post the whole method, and the full text of reek's warning message? I can't get this to report FeatureEnvy with the current version of reek. – kevinrutherford Jul 26 '09 at 20:36

1 Answers1

2

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
  • 73,644
  • 13
  • 135
  • 118
  • 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 Jul 26 '09 at 20:40