Skip to content

Instantly share code, notes, and snippets.

@itspriddle
Created July 22, 2011 00:22
Show Gist options
  • Select an option

  • Save itspriddle/1098559 to your computer and use it in GitHub Desktop.

Select an option

Save itspriddle/1098559 to your computer and use it in GitHub Desktop.
module AttrReaderWriter
def attr_reader_or_writer(*keys)
keys.flatten.each do |key|
class_eval <<-RUBY, __FILE__, __LINE__
def #{key}(val = nil)
@#{key} = val || @#{key}
end
RUBY
end
end
end
class Test
extend AttrReaderWriter
attr_reader_or_writer :name
end
t = Test.new
t.name #=> nil
t.name :foo
t.name #=> :foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment