Ruby 中密码检查的递归循环
好的,我正在尝试编写一个简单的对象,其中包含两个字符串,一个是“用户密码”,一个是“目标密码”,如果您想使用 sudo 在远程服务器上编写密码更改脚本(第一个密码是执行 sudo 命令,“目标密码”是应将密码重置为的字符串,
我希望提示用户一次输入第一个密码,然后用户将有五次尝试输入的机会。第二个密码字符串并准确地重复它,下面的代码似乎不起作用?
require 'pp'
require 'RubyGems'
require 'highline/import' #gem install highline
class Authorization
attr_reader :user_password , :target_password
pass_code = lambda {
first_attempt = ask("Enter target password: "){ |q| q.echo = '*' }
second_attempt = ask("Re-enter password to verify"){ |q| q.echo = '*'}
}
### So we need some sort of recursive loop
def initialize(target_pass=false)
@user_password = ask("Enter your admin password: ") { |q| q.echo = '*' }
if target_pass
count = 1
while n < 6
pass_code
if first_attempt == second_attempt
@target_password = first_attempt
return
else
count += 1
end
end
end
end
end
my_pass = Authorization.new(true)
pp "pass" , my_pass
OK, I am trying to write a simple object that will contain two strings, one a "user password" and one a "target password," this would be needed if you wanted to script a password change on a remote server using sudo (the first password would be to perform the sudo command, the "target password" would be the string to which the password should be reset.
I want the user to be prompted once for the first password, and then the user will have five tries to enter a second password string and repeat it accurately. What I came up with, the code below, does not seem to work. Any ideas?
require 'pp'
require 'RubyGems'
require 'highline/import' #gem install highline
class Authorization
attr_reader :user_password , :target_password
pass_code = lambda {
first_attempt = ask("Enter target password: "){ |q| q.echo = '*' }
second_attempt = ask("Re-enter password to verify"){ |q| q.echo = '*'}
}
### So we need some sort of recursive loop
def initialize(target_pass=false)
@user_password = ask("Enter your admin password: ") { |q| q.echo = '*' }
if target_pass
count = 1
while n < 6
pass_code
if first_attempt == second_attempt
@target_password = first_attempt
return
else
count += 1
end
end
end
end
end
my_pass = Authorization.new(true)
pp "pass" , my_pass
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我看到几个问题
require "rubygems"
(不是 RubyGems)尝试这样的事情。
I see several problems
require "rubygems"
(not RubyGems)Try something like this instead.
非常简单,与 Ryan 的解决方案类似:
Pretty straightforward and similar to Ryan's solution:
首先,感谢大家的回答。这是我的第一个问题,不幸的是,结果有点混乱,但大家似乎都很好地理解了它。
恕我直言,我试图做递归,但我不确定这是讨论的最佳场所。
我正在使用 Ruby 1.8.7,我可能应该在帖子开头提到它。 Ryan 的解决方案有效,但只有当我取出对“self”的引用并替换为实例变量时:
这对于 Ruby 1.9 可能不是必需的,但它确实具有使 protosuper 与 Ryan 几乎相同的优点。
再次感谢大家!这是一次很棒的“涉足”经历。
First, thank you all for your answers. This was my first question, and, unfortunately, came out a little garbled, but all of you seemed to understand it really well.
IMHO, what I was trying to do recursion, but I am not sure that this is the best place for that discussion.
I am using Ruby 1.8.7, which I probably should have mentioned at the beginning of the post. Ryan's solution worked, but only when I took out the references to "self" and substituted in the instance variable:
this might not be necessary for Ruby 1.9, but it does have the advantage of making prostosuper's almost identical to Ryan's.
Once again, thank you all! This has been a great "getting my feet wet" experience.
首先,这不是递归。这是迭代。递归函数会再次调用自身:
现在,针对您遇到的具体问题;您正在尝试使用以下条件循环:
但请注意,在循环体中,您没有执行任何操作来更改
n
的值。所以你的循环无法终止。 (此外,由于您一开始就忘记给n
赋值,所以它也可能无法启动。:)First, this isn't recursion. This is iteration. A recursive function would call itself again:
Now, for the specific problem you've got; you are trying to loop with the condition:
But note that in the body of your loop, you're doing nothing to change the value of
n
. So your loop cannot terminate. (Further, since you've forgotten to assign a value ton
in the first place, it probably cannot start, either. :)