Skip to content

Instantly share code, notes, and snippets.

@tatey
Created February 17, 2010 10:44
Show Gist options
  • Save tatey/306503 to your computer and use it in GitHub Desktop.
Save tatey/306503 to your computer and use it in GitHub Desktop.
# attr_accessor is "syntatic sugar" for dynamically defining attribute
# readers and accessors. Foo and Bar are functionally equivilant.
#
# Remember @instance_variables are only visible in the scope
# of the instance. If you need to access them from outside the instance,
# they must be exposed through accessors.
class Foo
attr_accessor :bar
end
foo = Foo.new
foo.bar = 'Baz'
foo.bar # => 'Baz'
class Bar
def foo=(value)
@foo = value
end
def foo
@foo
end
end
bar = Bar.new
bar.foo = 'Baz'
bar.foo # => 'Baz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment