1

I have a class defined in a gem to which I am adding some methods via refinements (in Ruby 2.3.0). This class turns up in some Sinatra views (haml).

When I refer to these extra methods in a helper, there is not a problem. But in the view, I get an Undefined Method error.

Am I missing a trick, or is it that the using ... statement would need to go somewhere I just can't get to?

(Workaround: I can define helper methods to return the method on the object. But if I wanted to do that then I wouldn't have used refinements...)

Andy Jones
  • 1,074
  • 1
  • 10
  • 21

1 Answers1

0

The scope of refinement is determined lexically. Unless you rewrite the method inside haml that calls that method so that it becomes within the scope of the using command, you cannot use refinements. But I guess haml is internally using eval or something like that to evaluate the code you write in a haml file. In that case, it is impossible.

sawa
  • 165,429
  • 45
  • 277
  • 381
  • Hmm. Not entirely impossible, then, since by default haml is rendered in the binding of the object you pass to it: `Haml::Engine.new(file).render(self)`... but of course if the Sinatra code doesn't cater to it, I'm screwed for now. – Andy Jones Mar 04 '16 at 12:19
  • ....no, wait, the `using` would have to go in the `Haml::Engine` class, wouldn't it? Damn. – Andy Jones Mar 04 '16 at 12:33