Questions tagged [refinements]

Refinements are a feature of Ruby intended to reduce the impact of monkey patching on other users of the monkey-patched class. Refinements provide a way to extend a class locally rather than globally.

Refinements are a feature of Ruby intended to reduce the impact of monkey patching on other users of the monkey-patched class. Refinements provide a way to extend a class locally rather than globally.

Due to Ruby’s open classes you can redefine or add functionality to existing classes. But as long as the scope of such changes is global, unintended side-effects or breakage of programs may occur.

Read more on the docs.

42 questions
3
votes
1 answer

Why does the Rails executor not recognize refinements on Kernel?

update that's burried in an answer comment below: the problem was happening in ruby 2.7, this is fixed in ruby 3 I discovered that if code is run within a Rails executor, refinements are not recognized. It doesn't seem to be because of blocks or…
John Bachir
  • 22,495
  • 29
  • 154
  • 227
3
votes
2 answers

Using refinements to patch a core Module such as Kernel

I'm going through the Facets API and picking some methods to include in my refinement-compatible patch library. I've hit a snag trying to patch Kernel. It's a module, whereas the other stuff I've patched has been classes (String, Array,…
max pleaner
  • 26,189
  • 9
  • 66
  • 118
2
votes
1 answer

Does dynamic dispatching decrease the cyclomatic complexity?

Imagine the following method converting a boolean into an int: def bool_to_int(bool) bool ? 1 : 0 end bool_to_int(false) # => 0 bool_to_int(true) # => 1 Because of the conditional, the cyclomatic complexity score is two. Does the score drop to…
Linus Oleander
  • 17,746
  • 15
  • 69
  • 102
2
votes
2 answers

Is it possible to use ruby refinements to change the behavior of a controller action in tests?

Is it possible to use the refinements feature to stub a controller action? I am defining the refinement in "my_controller_refinement.rb" require "my_controller" module MyControllerRefinement refine MyController do def create_item(my_object =…
2
votes
1 answer

How can "super" within a refinement call an overridden method?

I was under the impression that refinements fell outside the usual inheritance scheme in Ruby; that overriding a method within a refinement replaced the original method for all code using the refinement. But then, I tried this experiment with super,…
feralmosquito
  • 85
  • 1
  • 4
2
votes
1 answer

Wrap block to use refinement

Given the following refinement: module StringRefinement refine String do def bar length end end end I want to implement a module to execute blocks using my refinement: module Demo using StringRefinement def self.wrap(*args,…
SystematicFrank
  • 16,555
  • 7
  • 56
  • 102
1
vote
1 answer

How to use a refinement to change the output of { now: Time.now }.inspect to be customized?

After years of working with ruby, I finally came across a good need for ruby refinement! # frozen_string_literal: true module TimeMongoInspect refine Time do def inspect strftime("Time.parse('%Y-%m-%d %H:%M:%S %z')") end def…
Daniel
  • 7,006
  • 7
  • 43
  • 49
1
vote
0 answers

How can you refine HTTP::Headers.normalize_header?

normalize_header in HTTP::headers turns underscores into dashes (https://github.com/httprb/http/issues/337). This isn't correct in some cases. I can easily monkey patch this. But I was trying to figure out how to use a refinement to narrow the…
Michael Hunter
  • 414
  • 3
  • 8
1
vote
1 answer

Monkey-patching methods only inside my own code (by automatically using refinements?)

In my Ruby (on Rails) project, I’d like to forbid or restrict the usage of some methods provided by the standard library. Examples: I’d like to forbid calling Float#to_d because I had a rounding error when someone was using that method on a Float…
Manuel Jacob
  • 1,897
  • 10
  • 21
1
vote
0 answers

NoMethodError when using Refinements in an ActiveRecord Model

Ruby: 2.3.7 Rails: 4.2.10 I have the following implementation where I want to use Refinements to add a method to String class module StringExtensions refine String do def numeric? str = String.new(self) (/\A\d+\z/ =~ str) == 0 …
abhinavmsra
  • 666
  • 6
  • 21
1
vote
1 answer

Scoped monkey-patching of a class

I need to patch a class, but want the patch be local to some module(s). In Ruby I would do: module ArrayExtension refine Array do def ===(other) self.include?(other) end end end module Foo using ArrayExtension def self.foo …
WPeN2Ic850EU
  • 995
  • 4
  • 8
1
vote
0 answers

method added via refinement not visible in rails controller

I have refined the Enumerable module in a Rails controller as follows, adding one method that, as far as I know, doesn't already exist in Rails or the standard library: module OrderedGroupBy refine Enumerable do def ordered_group_by(&block) …
jars
  • 145
  • 2
  • 8
1
vote
1 answer

How to get the value from a Scott encoded GADT with type equality constraints?

I am reading the Rank-N-Types section of 24 days of GHC Extensions and came across the following GADT: {-# LANGUAGE GADTs #-} {-# LANGUAGE KindSignatures #-} import Data.Char data Some :: * -> * where SomeInt :: Int -> Some Int SomeChar…
user6445533
1
vote
1 answer

How to refine a method from a class inside a module

I want to refine a methoded located in a class inside a module, but despite the fact of trying to refine the class, I cannot have the refined code to run: module App module Entities class User def self.name(name) puts "Original…
Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
1
vote
1 answer

Ruby Enumerator "Single" method

I really need to make use of something similar to the Single method, which: Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence. Obviously I can add an extension/refinement for…
Dmytrii Nagirniak
  • 23,696
  • 13
  • 75
  • 130