Skip to content

Instantly share code, notes, and snippets.

@kaineer
Created May 16, 2014 10:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaineer/05107370ddc627ed5d43 to your computer and use it in GitHub Desktop.
Save kaineer/05107370ddc627ed5d43 to your computer and use it in GitHub Desktop.
How to be lazy and shut your leg down
class NotSoLazyGardener
def apples
@apples ||= []
end
def pears
@pears ||= []
end
# Good luck in debugging this one
def fruits
@fruits = apples + pears
end
end
lazy = LazyGardener.new
lazy.apples << 1
lazy.pears << 2
lazy.fruits # => [1, 2]
lazy.apples << 3
lazy.fruits # => [1, 2] # WTF?
lazy = NotSoLazyGardener.new
lazy.apples << 1
lazy.pears << 2
lazy.fruits # => [1, 2]
lazy.apples << 3
lazy.fruits # => [1, 2, 3] # yeah, right!
class LazyGardener
def apples
@apples ||= []
end
def pears
@pears ||= []
end
# Good luck in debugging this one
def fruits
@fruits ||= apples + pears
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment