Ruby 方法不可见

Ruby method not being seen

我有一个方法 known_args 从我的脚本的主要 class 调用,它包含在模块 Validations 中,但由于某种原因我得到:

undefined method `known_args' for PasswordGenerator:Class (NoMethodError)

奇怪的是,即使我把它放回主class,我也会得到同样的错误。

password_generator.rb

require 'securerandom'
require_relative 'validations'

class PasswordGenerator

    include Validations

    class Array
    # If +number+ is greater than the size of the array, the method
    # will simply return the array itself sorted randomly
      def randomly_pick(number)
            sort_by{ rand }.slice(0...number)
        end
    end

    def password_generator length, charsets
        #Set allowed characters
        valid_characters = charsets
        #Intitialise empty string
        password = ""
        length.times do
            #we cannot do "randomly_pick(length)" because we want repeating characters 
            password << valid_characters.randomly_pick(1).first
        end
        #Return password
        password
    end

    def username_generator
        SecureRandom.urlsafe_base64(calc_usename_length)
    end

    def calc_usename_length
        length = (12..32).to_a
        length.sample
    end
    ############### Input, checks and validations
    def charsets
        special_chars = [ '@', '£', "#", "%", "?", "€", "-", "+", "=", "*", "_", "~", "^", "|", ".", "[","]", "{", "}" ]
        other = ('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a
        combined = use_special? ? insert_special(special_chars, other) : no_special(other)
        combined
    end

    def use_special?
        return true if @use_special_argument == 's' || @use_special_argument == 'S'
        return false if @use_special_argument == 'b' || @use_special_argument == 'B'
    end

    def insert_special(special_chars, other)
        other + special_chars
    end

    def no_special(other)
        other
    end

    def argument_check length
        is_integer(length) && check_length(length) ? pass(length) : fail
    end

    def pass length
        password_generator(length, charsets) 
    end

    length = ARGV[0].to_i
    @use_special_argument = ARGV[1]

    unless known_args.include? @use_special_argument
        puts
        puts "Specify whether or not to use special characters."
        puts "Use special characters: 's' || 'S'"
        puts "No special characters: 'b' || 'B'"
        puts
        puts "Unknown argument: '#{@use_special_argument}'. Aborting"
        exit_script
    end

    puts
    puts "Username: " + username_generator
    puts "Password: " + argument_check(length)
    puts
    puts "Complete. Exiting"
    exit_script

end


validations.rb

    module Validations

    def is_integer length
        length.is_a? Integer
    end

    def check_length length
        length > 0
    end

    def fail
        'Argument must be integer and greater than 0. Recommend max length available by application. 64 if unknown'
    end

    def known_args
        known_args_array = ['s', 'S', 'b', 'B' ]
        known_args_array
    end

    def exit_script
        Kernel.exit(false)
    end

end

仔细查看错误信息:

undefined method `known_args' for PasswordGenerator:Class (NoMethodError)

该消息说 Ruby 在 PasswordGenerator 中找不到 known_args 方法,在 PasswordGenerator.

的实例中找不到

include ValidationsValidations 添加 known_args 作为实例方法,但您试图将其作为 class 方法调用。

您可以设置一个 Validations.included 方法来在 include Validations 发生时添加 known_args 包含程序,或者,更好的 IMO,将所有代码移动到一个实例方法中:

class PasswordGenerator
  #...

  def perform
    length = ARGV[0].to_i
    @use_special_argument = ARGV[1]

    unless known_args.include? @use_special_argument
        puts
        puts "Specify whether or not to use special characters."
        puts "Use special characters: 's' || 'S'"
        puts "No special characters: 'b' || 'B'"
        puts
        puts "Unknown argument: '#{@use_special_argument}'. Aborting"
        exit_script
    end

    puts
    puts "Username: " + username_generator
    puts "Password: " + argument_check(length)
    puts
    puts "Complete. Exiting"
    exit_script
  end
end

然后说:

PasswordGenerator.new.perform

把事情搞砸。