Create a gist now

Instantly share code, notes, and snippets.

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 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