Skip to content

Instantly share code, notes, and snippets.

@rogercampos
Created July 21, 2012 22:28
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 rogercampos/3157425 to your computer and use it in GitHub Desktop.
Save rogercampos/3157425 to your computer and use it in GitHub Desktop.
while behaviour
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.
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