Created
August 18, 2010 18:19
-
-
Save tjsingleton/535692 to your computer and use it in GitHub Desktop.
Example of the difference between a side effect free method and a method with side effects.
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
class Value | |
attr_reader :value | |
def initialize(value) | |
@value = value | |
end | |
def add(i) | |
class.self.new(i + @value) | |
end | |
end | |
value = Value.new(1) | |
value2 = value.add(1) | |
value.value == 1 | |
value2.value == 2 |
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
class Value | |
def initialize(value) | |
@value = value | |
end | |
def add(i) | |
@value = i + @value | |
end | |
end | |
value = Value.new(1) | |
value.add(1) | |
value.value = 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment