Skip to content

Instantly share code, notes, and snippets.

@DavidEGrayson
Created January 13, 2013 22:05
Show Gist options
  • Save DavidEGrayson/4526439 to your computer and use it in GitHub Desktop.
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 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