rubysinatrarefinements

Is it possible to use methods defined in refinements in Sinatra views?


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...)


Solution

  • 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.