For a long time I’ve been looking for a way to use Ruby’s refinements without building a class & method setup and then instantiating an instance of the class just to use it. And now I’ve finally found a solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
module Moo refine Fixnum do def to_s "moo" end end end # begin/end blocks allow access to local variables begin # valid Ruby 2.2.3 and NOT Ruby 2.3.0 using Moo 1.to_s end # => "moo" # class syntax has no access to outer local variables class << Class.new # valid Ruby 2.3.0 using Moo 1.to_s end # => "moo" |
The best work around I’ve found for accessing values outside the class wood be…
Continue Reading »