0

Is there any way to limit the effect of a refinement in a single ruby program apart from using it within a module?

For example, let's say the name of the refinement is StringRefinement and as we type using StringRefinement it comes into effect and is in effect till the end of the program file.

Is there any way to limit its boundary so that some later part of the program doesn't have effects of that refinement?

undur_gongor
  • 15,657
  • 5
  • 63
  • 75
amritesh
  • 83
  • 6

1 Answers1

1

Just wrap your application in a module which uses the refinement:

module MyApp
  using StringRefinement

  def self.run!
    # Do your job
  end
end
Aetherus
  • 8,720
  • 1
  • 22
  • 36
  • Interesting! When was that functionality added back in? It was in the original refinements implementation, but it was removed before the release of Ruby 2.0, because the developers of JRuby, Rubinius, MagLev, MacRuby, IronRuby etc. complained that it would make optimizations such as speculative inlining impossible. YARV doesn't do any optimizations at all, so it doesn't care, but the others very much *do* care about such optimizations, so refinements were stripped to the bare essentials for the 2.0 release. I hadn't noticed that they were beefed up again, was that in 2.2? – Jörg W Mittag May 12 '15 at 11:34
  • Ah, found it. Refinements were … uh … refined in Ruby 2.1. – Jörg W Mittag May 12 '15 at 11:42