Created
January 13, 2013 22:05
-
-
Save DavidEGrayson/4526439 to your computer and use it in GitHub Desktop.
These are my ideas for how to implement helpers using Ruby 2.0 refinements. See http://blog.davidegrayson.com/2013/01/ruby-20-helpers-should-be-refinements.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This fails. I think the refinements are loaded into the scope | |
# only when #using is called, and additional refinements that are | |
# added to the Helpers module later have no effect. | |
module Helpers | |
refine Class do | |
def helper(mod) | |
klass = self | |
Helpers.module_exec do | |
puts "#{klass} wants to use #{mod}, self=#{self}" | |
refine(klass) { include mod } | |
end | |
end | |
end | |
end | |
module MyHelper | |
def add(a,b) | |
a + b | |
end | |
end | |
using Helpers | |
class MyClass | |
helper MyHelper | |
def join(str1, str2) | |
add str1, str2 | |
end | |
end | |
puts MyClass.new.join("a", "b") # => NoMethodError for 'add' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment