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
class Universe | |
def initialize | |
@answer = "42" | |
end | |
end | |
universe = Universe.new # => #<Universe:0x007f4b34292288 @answer="42"> | |
universe.instance_variables # => [:@answer] |
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
class Universe | |
def initialize | |
@answer = "42" | |
end | |
end | |
universe = Universe.new # => #<Universe:0x007f4b34292288 @answer="42"> | |
universe.instance_variables # => [:@answer] |
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
class Universe | |
def answer=(something) | |
@answer = something | |
end | |
end | |
universe = Universe.new # => #<Universe:0x007f639f31b8e8> | |
universe.instance_variables # => [] | |
universe.answer = "42" # => "42" | |
universe.instance_variables # => [:@answer] |
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
class Universe | |
attr_writer :answer | |
end | |
universe = Universe.new # => #<Universe:0x007f9ac4572e78> | |
universe.instance_variables # => [] | |
universe.answer = "42" # => "42" | |
universe.instance_variables # => [:@answer] |
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
class Universe | |
def initialize | |
@answer = "42" | |
puts @answer # Get @answer and pass it to Kernel#puts. | |
end | |
end | |
universe = Universe.new # printed 42 and returned <Universe:0x007fcdef8fb000 @answer="42"> |
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
class Universe | |
def initialize | |
@answer = "42" | |
end | |
def answer | |
return @answer # Explicit return for clarity | |
end | |
end |
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
class Universe | |
attr_reader :answer | |
def initialize | |
@answer = "42" | |
end | |
end | |
universe = Universe.new # => #<Universe:0x007f644d49de10 @answer="42"> | |
universe.answer # => "42" |
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
class Universe | |
attr_accessor :answer | |
def initialize | |
@answer = "42" | |
end | |
end | |
universe = Universe.new # => #<Universe:0x007f63e303e848 @answer="42"> | |
universe.answer # => "42" |
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
@answer = "42" # => "42" | |
@answer.object_id # => 70033493776200 | |
@answer.clear # => "" | |
@answer # => "" | |
@answer.object_id # => 70033493776200 |
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
def answer=(value) | |
@answer = value | |
end |
OlderNewer