Skip to content

Instantly share code, notes, and snippets.

@henrik
Created January 14, 2015 20:06
Show Gist options
  • Save henrik/ba9c820e7ef9846ae9c9 to your computer and use it in GitHub Desktop.
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.
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