Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5943917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:27:50+00:00 2026-05-22T16:27:50+00:00

OK, I am trying to write a simple object that will contain two strings,

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:27:51+00:00Added an answer on May 22, 2026 at 4:27 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple component that will allow you to embed one
I'm trying to write a simple WPF app that has two ellipses, joined by
I'm trying to write a simple ruby script that will copy a file to
I am trying to write a simple tool using Shoes. This will indent code
I'm trying to write some code that will take any object and convert it
I'm trying to write a simple program that will compare the files in separate
I'm trying to write a simple app that will do a screen capture of
I'm trying to write some simple C# code that will select some data from
I'm trying to write simple DAO that will create entity objects based on their
I'm trying to write a simple Ubiquity command that will run a query on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.