refinements

Why do Ruby refinements only modify classes, not modules?

匆匆过客 提交于 2019-12-23 12:55:37
问题 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 my_method # => hello module MyModule def my_method "goodbye" end end puts my_method # => goodbye I'm sure this isn't a good idea, but it might not be as bad if you could limit the scope of such a monkey patch. So why can't you? 回答1: The refine in Ruby is

Using Refinements Hierarchically

烂漫一生 提交于 2019-12-23 10:09:31
问题 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 will simplify thus: a = [[1, "a"], [2, "b"], [3, "c"], [4, "d"]] b = [[1, "AA"], [2, "B"], [3, "C"], [5, "D"]] The element at offset i in a matches the element at offset i in b if: a[i].first == b[i].first and a[i].last.downcase == b[i].last

Are refinements in Ruby 2.0 totally useless? [closed]

一笑奈何 提交于 2019-12-13 10:42:33
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . There were so-called refinements introduced in Ruby 2.0. I was playing with them and now I’m totally cajoled: — The main declared

Why doesn't to_proc work inside Ruby refinements?

早过忘川 提交于 2019-12-12 10:52:22
问题 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, 3]].map(&:sum) # => array.rb:13:in `map': undefined method `sum' for [1, 2, 3]:Array (NoMethodError) puts [1, 2, 3].method(:sum).to_proc.call # => array.rb:14:in `method': undefined method `sum' for class `Array' (NameError) Is this the intended behavior? Is there a

How to share code between ruby refinements?

♀尐吖头ヾ 提交于 2019-12-11 13:57:22
问题 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 and bar between Hash and Array by using include , it fails. module Shared def foo puts :in_foo end def bar puts :in_bar foo end end module Ext refine Hash do include Shared end refine Array do include Shared end end module Test using Ext Hash.new.bar end # in_bar # NameError: undefined local

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

二次信任 提交于 2019-12-03 03:41:48
问题 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? } end self.send :include, _refinement end end and the following test passes class Base def foo "foo" end end class Receiver include_refined(Base) { def foo "refined " + super end } end describe Receiver do it { should respond_to(:foo) } its(:foo) { should eq("refined foo") }