Created
December 6, 2019 06:39
-
-
Save havenwood/e459ca5ccde8375a781563649926ba33 to your computer and use it in GitHub Desktop.
Examples of defining methods and getting and setting instance variables on objects
This file contains 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
foo = Object.new | |
def foo.value | |
42 | |
end | |
foo.value | |
#=> 42 | |
bar = Object.new | |
class << bar | |
def value | |
43 | |
end | |
end | |
bar.value | |
#=> 43 | |
baz = Object.new | |
def baz.value | |
@value | |
end | |
baz.instance_variable_set(:@value, 44) | |
baz.value | |
#=> 44 | |
wombat = Object.new | |
wombat.instance_variable_set(:@value, 45) | |
wombat.instance_variable_get(:@value) | |
#=> 45 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment