Created
October 22, 2012 21:02
-
-
Save syntacticsugar/3934260 to your computer and use it in GitHub Desktop.
Ruby Or-Equals ||= weirdness
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
@factorial_results = {} | |
def factorial(num) | |
start = Time.now | |
raise "num has to be greater than 0" if num < 0 | |
return 1 if num == 0 | |
if @factorial_results.has_key?(num) | |
puts "found it!" | |
puts Time.now - start | |
return @factorial_results[num] | |
end | |
puts "calculating..." | |
result = 1 | |
num.downto(1) do |i| | |
result = result * i | |
end | |
@factorial_results[num] = result | |
puts "done!" | |
puts Time.now - start | |
result | |
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
Death-Lair-of-Killer-Bambi:moodphile stickycake$ pry | |
[1] pry(main)> qwer | |
NameError: undefined local variable or method `qwer' for main:Object | |
from (pry):1:in `__pry__' | |
[2] pry(main)> qwer || 5 | |
NameError: undefined local variable or method `qwer' for main:Object | |
from (pry):2:in `__pry__' | |
[3] pry(main)> a = qwer || 5 | |
NameError: undefined local variable or method `qwer' for main:Object | |
from (pry):3:in `__pry__' | |
[4] pry(main)> qwer = qwer || 5 | |
=> 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment