I’m trying to use a regular expression as a condition inside (until) loop.
basically, It’s for entering numeric password..
I tried this code
print "Password: "
x = gets.chomp.to_i
until (/^[\d]+(\.[\d]+){0,1}$/ === "#{x}") == "true"
print "Only numbers allowed, Password: "
x = gets.chomp.to_i
end
but unfortunately it didn’t work.
Any ideas?
You shouldn’t compare to the string
"true". In fact in Ruby you barely ever should need to explicitly compare totrueorfalse, since that’s what boolean expressions evaluate to. Also note thatto_iprobably does not do what you expect:What you can do is something like this: