Question

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.

Was it helpful?

Solution

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]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top