【发布时间】:2014-03-18 01:00:20
【问题描述】:
我正在尝试使用正则表达式并在 ruby 中测试我自己的代码。使用下面的示例,我预计最终的 puts 会返回 true,但事实并非如此。但是,check_password 方法会返回“您的密码太短,无法正常工作”。为什么真/假检查不返回 true?
def err_message(reason)
puts "Your pw does not work because " + reason
end
def check_password(password)
if password.length<6
return err_message("it is too short")
elsif password.index(/[A-Z]/)==nil
return err_message("it does not contain a capital letter")
elsif password.index(/\d|[!,@,#,$,%,&,*,+,:,?]/)==nil
return err_message("it needs either a digit or special character")
elsif password.index(/([!,@,#,$,%,&,*,+,:,?\w])/)>0
return err_message("nope. !,@,#,$,%,&,*,+,:,? are the only special characters available")
else
return "Valid Password!"
end
end
puts check_password("aaaaa")=="Your pw does not work because it is too short"
【问题讨论】: