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
26
votes
2 answers

Better way to turn a ruby class into a module than using refinements?

Module#refine method takes a class and a block and returns a refinement module, so I thought I could define: class Class def include_refined(klass) _refinement = Module.new do include refine(klass) { yield if block_given? …
Andrea Amantini
  • 480
  • 4
  • 11
16
votes
2 answers

Do refinements apply only to instance methods?

I'm trying to understand Ruby's refinements feature, and I've encountered a scenario I don't understand. Take this example code: class Traveller def what_are_you puts "I'm a Backpacker" end def self.preferred_accommodation puts…
andrewdotnich
  • 16,195
  • 7
  • 38
  • 57
10
votes
2 answers

How do I use Ruby refinements in Rails views?

I have a Rails 4 project using Ruby 2.0. I've defined some refinements. Putting <% using MyRefinements %> at the top of the view files causes the error "undefined method 'using'". When I add: using MyRefinements At the top of my controller…
Brad Urani
  • 1,429
  • 1
  • 16
  • 29
9
votes
1 answer

Ruby refinements gotchas

In Metaprogramming Ruby 2 in Chapter 2 at the "Refinements" section I found the following piece of Ruby code: class MyClass def my_method "original my_method()" end def another_method my_method end end module MyClassRefinement …
stratis
  • 7,750
  • 13
  • 53
  • 94
8
votes
1 answer

Ruby refinements and hooks

I'm trying to use ruby refinements to apply rails hooks. I want to avoid monkey patching. When monkey patching it would work as such ActiveRecord::Base.class_eval do after_find do # do something with my_method end def my_method …
Max
  • 156
  • 1
7
votes
1 answer

Why did I get "main.using is permitted only at toplevel" when I used a Refinement in IRB?

I tried to use a Refinement in IRB (v0.9.6, Ruby 2.3.0): module Foo refine Object do def foo() "foo" end end end using Foo # => RuntimeError: main.using is permitted only at toplevel This is basically the exact setup from the documentation…
user513951
  • 12,445
  • 7
  • 65
  • 82
7
votes
1 answer

Ruby refinements subtleties

There is a pretty good documentation of the current implementation of refinements in ruby here: http://ruby-doc.org//core-2.2.0/doc/syntax/refinements_rdoc.html, but there are some strange corner cases. First, include module is orthogonal to using…
5
votes
2 answers

Why doesn't to_proc work inside Ruby refinements?

It seems that to_proc doesn't work on methods defined in refinements: module ArrayExtensions refine Array do def sum reduce(0, :+) end end end using ArrayExtensions puts [[1, 2, 3]].map { |array| array.sum } # => 6 puts [[1, 2,…
Patrick Brinich-Langlois
  • 1,381
  • 1
  • 15
  • 29
5
votes
1 answer

Testing class which uses refinements with RSpec

Let's say I've got refinement module RefinedString refine String do def remove_latin_letters #code code code code end end end and I use it inside my class Speech: class Speech using RefinedString def initialize(text) …
Filip Bartuzi
  • 5,711
  • 7
  • 54
  • 102
5
votes
1 answer

Using Refinements Hierarchically

Refinements was an experimental addition to v2.0, then modified and made permanent in v2.1. It provides a way to avoid "monkey-patching" by providing "a way to extend a class locally". I attempted to apply Refinements to this recent question which I…
Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
4
votes
0 answers

Applying refinements to a block

Let's say I'm designing a domain-specific language. For this simplified example, we have variables, numbers, and the ability to add things together (either variables or numbers) class Variable attr_reader :name def initialize(name) @name =…
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
4
votes
1 answer

Refine gem's class method

I have to wrap some behavior around an external gem in a elegant and isolated manner. Given the abstraction below, everything runs smoothly, but 'bar' is never printed. Could someone tell me why? My code: module RefineGem refine…
fabriciofreitag
  • 2,843
  • 22
  • 26
4
votes
1 answer

best way to organize a long piece of code in ruby refinement block

module Access def last self[-1] end def start_end self[0] + last end end module StringExt refine String do include Access end end using StringExt puts 'abcd'.last # => d puts 'abcd'.start_end When a class being…
4
votes
3 answers

Ruby refinements with instance_eval

I'd like to provide some refinements to a DSL. I'm able to get refinements working with this example: module ArrayExtras refine Array do def speak puts 'array!' end end end module MyUniverse using ArrayExtras class Thing …
Andy
  • 285
  • 3
  • 12
4
votes
2 answers

Why do Ruby refinements only modify classes, not modules?

The Ruby docs on refinements state: Refinements only modify classes, not modules so the argument must be a class. Why is this? It's possible to monkey-patch a module: module MyModule def my_method "hello" end end include MyModule puts…
feralmosquito
  • 85
  • 1
  • 4
1
2 3