Ruby 中密码检查的递归循环

发布于 2024-11-09 05:26:50 字数 975 浏览 0 评论 0原文

好的,我正在尝试编写一个简单的对象,其中包含两个字符串,一个是“用户密码”,一个是“目标密码”,如果您想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼波传意 2024-11-16 05:26:50

我看到几个问题

  • 它是 require "rubygems" (不是 RubyGems)
  • 另外,如果使用 Ruby 1.9,则不需要加载 ruby​​gems。
  • lambda 分配了局部范围的变量,这些变量在构造函数中不可用
  • 无论如何,lambda 定义本身超出了访问范围
  • 循环永远不会终止(顺便说一句,这不是递归)。

尝试这样的事情。

require "highline/import"

class Authorization
  attr_accessor :user_password, :target_password

  def prompt(prompt_for_target = false)
    self.user_password = ask_for_password("Enter your admin password")
    return unless prompt_for_target

    5.times do
      password     = ask_for_password("Enter target password")
      confirmation = ask_for_password("Re-enter password to verify")

      if password == confirmation
        self.target_password = password
        return
      end
    end
  end

  private

  def ask_for_password(message)
    ask("#{message}: ") { |q| q.echo = '*' }
  end
end

auth = Authorization.new
auth.prompt(true)

puts auth.user_password
puts auth.target_password

I see several problems

  • It's require "rubygems" (not RubyGems)
  • Also, if using Ruby 1.9, loading rubygems isn't necessary.
  • The lambda has locally scoped variables assigned that aren't available in the constructor
  • The lambda definition itself is out of scope for access anyway
  • The loop never terminates (btw, this isn't recursion).

Try something like this instead.

require "highline/import"

class Authorization
  attr_accessor :user_password, :target_password

  def prompt(prompt_for_target = false)
    self.user_password = ask_for_password("Enter your admin password")
    return unless prompt_for_target

    5.times do
      password     = ask_for_password("Enter target password")
      confirmation = ask_for_password("Re-enter password to verify")

      if password == confirmation
        self.target_password = password
        return
      end
    end
  end

  private

  def ask_for_password(message)
    ask("#{message}: ") { |q| q.echo = '*' }
  end
end

auth = Authorization.new
auth.prompt(true)

puts auth.user_password
puts auth.target_password
亢潮 2024-11-16 05:26:50

非常简单,与 Ryan 的解决方案类似:

require 'highline/import'  #gem install highline

class Authorization

  attr_reader :admin_password, :target_password

  def initialize
    @admin_password = ask_for_password("Enter your admin password: ")
    5.times do
      @target_password = ask_for_password("Enter target password: ")
      verify_target_pass = ask_for_password("Re-enter password to verify: ")
      break if @target_password == verify_target_pass
      @target_password = nil
    end

  end 

  private

  def ask_for_password(message)
    ask(message) {|q| q.echo = "*"}
  end

end

my_pass = Authorization.new
puts "Administrator's password is: #{my_pass.admin_password}"
puts "Target password is: #{my_pass.target_password}"

Pretty straightforward and similar to Ryan's solution:

require 'highline/import'  #gem install highline

class Authorization

  attr_reader :admin_password, :target_password

  def initialize
    @admin_password = ask_for_password("Enter your admin password: ")
    5.times do
      @target_password = ask_for_password("Enter target password: ")
      verify_target_pass = ask_for_password("Re-enter password to verify: ")
      break if @target_password == verify_target_pass
      @target_password = nil
    end

  end 

  private

  def ask_for_password(message)
    ask(message) {|q| q.echo = "*"}
  end

end

my_pass = Authorization.new
puts "Administrator's password is: #{my_pass.admin_password}"
puts "Target password is: #{my_pass.target_password}"
始于初秋 2024-11-16 05:26:50

首先,感谢大家的回答。这是我的第一个问题,不幸的是,结果有点混乱,但大家似乎都很好地理解了它。

恕我直言,我试图做递归,但我不确定这是讨论的最佳场所。

我正在使用 Ruby 1.8.7,我可能应该在帖子开头提到它。 Ryan 的解决方案有效,但只有当我取出对“self”的引用并替换为实例变量时:

@user_password = ask_for_password("Enter your admin password") #instead of
self.user_password = ask_for_password("Enter your admin password")  

这对于 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:

@user_password = ask_for_password("Enter your admin password") #instead of
self.user_password = ask_for_password("Enter your admin password")  

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.

瀞厅☆埖开 2024-11-16 05:26:50

首先,这不是递归。这是迭代。递归函数会再次调用自身:

def factorial(n)
    if (n == 0)
        1
    else
        n * factorial(n-1)
    end
end

现在,针对您遇到的具体问题;您正在尝试使用以下条件循环:

当 n < 6

但请注意,在循环体中,您没有执行任何操作来更改 n 的值。所以你的循环无法终止。 (此外,由于您一开始就忘记给 n 赋值,所以它也可能无法启动。:)

First, this isn't recursion. This is iteration. A recursive function would call itself again:

def factorial(n)
    if (n == 0)
        1
    else
        n * factorial(n-1)
    end
end

Now, for the specific problem you've got; you are trying to loop with the condition:

while n < 6

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 to n in the first place, it probably cannot start, either. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文