我从我的脚本的主类调用了一个方法known_args
,它包含在模块Validations
中,但出于某种原因,我得到了:
undefined method `known_args' for PasswordGenerator:Class (NoMethodError)
奇怪的是,即使我把它放回到主类中,我也会得到同样的错误。
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
答案 0 :(得分:2)
仔细查看错误消息:
undefined method `known_args' for PasswordGenerator:Class (NoMethodError)
该消息称Ruby无法在known_args
中找到PasswordGenerator
方法,而不是PasswordGenerator
的实例。
说include Validations
从known_args
添加Validations
作为实例方法,但您尝试将其称为类方法。
您可以设置Validations.included
方法,以便在known_args
发生时添加include Validations
包含者,或者更好的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
把事情搞砸了。