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
1
vote
1 answer

Is it possible to use methods defined in refinements in Sinatra views?

I have a class defined in a gem to which I am adding some methods via refinements (in Ruby 2.3.0). This class turns up in some Sinatra views (haml). When I refer to these extra methods in a helper, there is not a problem. But in the view, I get an…
Andy Jones
  • 1,074
  • 1
  • 10
  • 21
1
vote
1 answer

How to share code between ruby refinements?

module Ext refine Hash do def foo puts :in_foo end def bar puts :in_bar foo end end end module Test using Ext Hash.new.bar end # in_bar # in_foo # => nil This works as expected. But if I want to share foo…
chifung7
  • 2,531
  • 1
  • 19
  • 17
1
vote
2 answers

How do I use Ruby refinements inside Rails Controller/Action?

If I try outside the controller it works: using ParamsExtension class ApplicationController If I try inside the controller or an action it does not work: class ApplicationController using ParamsExtension It throws 'undefined method `using'. I read…
Jirico
  • 1,242
  • 1
  • 15
  • 29
0
votes
1 answer

Configurable refinements in ruby

I'd like to patch parts of an existing library with a config object: module Library class A end class B end end module Config def config(&block) @config ||= block&.call end end module Patch refine Library::A.singleton_class do …
23tux
  • 14,104
  • 15
  • 88
  • 187
0
votes
1 answer

Sharepoint Refinement Breadcrumb

As per Attached, How to create the refinement breadcrumb in SharePoint? Please help me. Thank you...
0
votes
1 answer

Difference between Refinements and redefine class in ruby

I'm reading some book for ruby programming language and was wondering how something like this works class String def word_count frequencies = Hash.new(0) downcase.scan(/\w+/) { |word| frequencies[word] += 1 } return frequencies …
Amr Adel
  • 574
  • 1
  • 7
  • 18
0
votes
1 answer

Refinements and namespaces

Trying to patch net/http and have it only apply to one service class. Refinements seem to be the way to go. The monkey patch below works but the refinement doesn't. Is this a namespace issue? The project is on ruby 2.3.0 but have tried with 2.4.1 as…
kreek
  • 8,774
  • 8
  • 44
  • 69
0
votes
1 answer

Google Custom Search: Some refinements have zero results

I have set up a google custom search engine for my site and it works really well. I have also set up refinements for different categories in the site. E.g. Cups, Saucers, Plates When I do a search using the api or search box, for "China Cups" google…
hitwill
  • 575
  • 1
  • 9
  • 25
0
votes
2 answers

endeca returning zero results in refinements when none of refinements available in ref app?

I am using Endeca 3.1.2 Assembler API. When I am hitting the Endeca query, its giving me some bunch of refinements which contains zero counts and some positive counts . Example: category **category1(0)** category2(25) …
spartans
  • 33
  • 1
  • 1
  • 8
0
votes
1 answer

Ruby2.0: What is the difference between Ruby Refinements and Monkeypatches?

I could do some simple task in either way, Refinements module StringRefinements refine String do def do_something "bla bla bla..." end end end So, I can use do_something method wherever StringRefinements module was…
Venkat Ch
  • 1,168
  • 2
  • 17
  • 37
0
votes
1 answer

Refinement in Ruby

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…
amritesh
  • 83
  • 6
-3
votes
1 answer

Are refinements in Ruby 2.0 totally useless?

There were so-called refinements introduced in Ruby 2.0. I was playing with them and now I’m totally cajoled: — The main declared advantage of refine is that they are not global scoped. Bah. module MyModule class ::String def…
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
1 2
3