Skip to content

Instantly share code, notes, and snippets.

@fronx
Created February 8, 2011 09:34
Show Gist options
  • Select an option

  • Save fronx/816174 to your computer and use it in GitHub Desktop.

Select an option

Save fronx/816174 to your computer and use it in GitHub Desktop.
class Class
def self.with_reader(name, &block)
Class.new do
attr_reader :"#{name}"
define_method :initialize do |value|
instance_variable_set("@#{name}", value)
end
class_eval(&block) if block_given?
end
end
end
Foo = Class.with_reader(:bar) do
def moo; "moo"; end
end
foo = Foo.new(123)
puts foo.bar
puts foo.moo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment