Created
July 21, 2012 22:28
-
-
Save rogercampos/3157425 to your computer and use it in GitHub Desktop.
while behaviour
This file contains hidden or 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
require "securerandom" | |
def get_secure_random | |
puts "Func called" | |
SecureRandom.hex(16) | |
end | |
res = get_secure_random while res[0] != "a" | |
----- Result: | |
cuca.rb:11:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError) | |
So, in the while evaluation here the `res` variable is nil, and the `get_secure_random` function is never called. |
This file contains hidden or 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
require "securerandom" | |
def get_secure_random | |
puts "Func called" | |
SecureRandom.hex(16) | |
end | |
begin | |
res = get_secure_random | |
end while res[0] != "a" | |
----- Result: | |
Func called # Repeat N times | |
"ad34b41ab0af4b2cdff238b3536fa5ef" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment