Created
May 16, 2014 10:29
-
-
Save kaineer/05107370ddc627ed5d43 to your computer and use it in GitHub Desktop.
How to be lazy and shut your leg down
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 NotSoLazyGardener | |
def apples | |
@apples ||= [] | |
end | |
def pears | |
@pears ||= [] | |
end | |
# Good luck in debugging this one | |
def fruits | |
@fruits = apples + pears | |
end | |
end |
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
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! | |
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 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