It's still an experimental feature, but refinements sound like what you're
after:

https://ruby-doc.org/core-2.1.1/doc/syntax/refinements_rdoc.html
https://blog.codeship.com/ruby-refinements/


On 27 April 2017 at 14:02, Peter Hickman <peterhickman386 / googlemail.com>
wrote:

> I have the need for things like nil + 12.0 to return nil in the way that
> some software packages propagate NaN and other oddities.
>
> In my code I have this sort of thing which solves the problem.
>
> class Float
>   alias old_plus +
>   def +(other); other ? old_plus(other) : other; end
> end
>
> class NilClass
>   def +(_other); self; end
> end
>
> Except that the patch is now permanent and will affect all parts of the
> application. If I were to image this I would like to write something like:
>
> # Here nil + 12.0 will throw an exception
>
> magic.do
>   alias Float + new_float_plus
>   alias NilClass + new_nil_plus
>
>   # This will now run without an exception and output nil
>   puts nil + 12.0
> end
>
> # Here again nil + 12.0 will throw an exception
>
> Basically I want to patch some classes, like Float and NilClass, run some
> code and then revert the patches in the middle of some code.
>
> Any ideas?
>
>
>
> Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
>
>
(supressed text/html)
Unsubscribe: <mailto:ruby-talk-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>