Skip to content

Instantly share code, notes, and snippets.

@tjsingleton
Created August 18, 2010 18:19
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 tjsingleton/535692 to your computer and use it in GitHub Desktop.
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.
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
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