Skip to content

Instantly share code, notes, and snippets.

@havenwood
Created December 6, 2019 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havenwood/e459ca5ccde8375a781563649926ba33 to your computer and use it in GitHub Desktop.
Save havenwood/e459ca5ccde8375a781563649926ba33 to your computer and use it in GitHub Desktop.
Examples of defining methods and getting and setting instance variables on objects
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