Created
February 17, 2010 10:44
-
-
Save tatey/306503 to your computer and use it in GitHub Desktop.
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
# 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