Created
January 14, 2015 20:06
-
-
Save henrik/ba9c820e7ef9846ae9c9 to your computer and use it in GitHub Desktop.
Toy implementation of a "static façade" in Ruby. Based on the thread https://twitter.com/henrik/status/555450649652248577.
This file contains hidden or 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
module StaticFacade | |
def static_facade(*arg_names, name) | |
arg_names.each { |x| attr_reader x; private x } | |
define_method(:initialize) do |*arg_values| | |
arg_names.zip(arg_values).each { |n, v| instance_variable_set("@#{n}", v) } | |
end | |
singleton_class.send(:define_method, name) do |*args| | |
new(*args).send(name) | |
end | |
end | |
end | |
class Example | |
extend StaticFacade | |
static_facade :name, | |
def yo | |
puts greeting | |
end | |
private | |
def greeting | |
"Yo #{name}" | |
end | |
end | |
Example.yo("mama") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment