Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby';是"a"和"x27 ;;需要类或模块(类型错误)_Ruby_While Loop_Integer_Conditional_Typeerror - Fatal编程技术网

Ruby';是"a"和"x27 ;;需要类或模块(类型错误)

Ruby';是"a"和"x27 ;;需要类或模块(类型错误),ruby,while-loop,integer,conditional,typeerror,Ruby,While Loop,Integer,Conditional,Typeerror,我正在玩Ruby并尝试创建一个小的银行账户程序。当我运行使用create_帐户运行的这一特定代码行时: unless @response.is_a? Integer && @response.to_str.length == 4 puts "Your response must be 4 numbers in length." create_account else @pin = @response puts "Your pin has been

我正在玩Ruby并尝试创建一个小的银行账户程序。当我运行使用create_帐户运行的这一特定代码行时:

unless @response.is_a? Integer && @response.to_str.length == 4
    puts "Your response must be 4 numbers in length."
    create_account
else
    @pin = @response
    puts "Your pin has been set."
end
我得到的答复是:

bank_account.rb:24:in 'is_a?':class or module required (TypeError)
    from bank_account.rb:24:in 'create_account'
    from bank_account.rb:47:in '<main>'
bank_account.rb:24:in'is_a':需要类或模块(TypeError)
来自银行账户。rb:24:in“创建账户”
来自银行账户。rb:47:in“”
我不知道到底发生了什么,但我包含了我的其余代码。这是不完整的,显然是因为我被困在这一部分。我通过while循环并输入“Create Account”来启动Create_Account方法。我的目标是运行条件语句,并要求用户键入一个4位整数,就像任何ole pin码一样。如果它的长度既不是整数也不是4位数,我调用create_account方法让它们重新开始


如果需要这些信息,我将运行ruby 2.0.0,但我敢打赌这可能更多地与我的代码有关。Stackoverflow对我来说是新鲜事,所以如果有人问这个问题,我很抱歉。我试着在提问之前按照建议做家庭作业,但我还是被难倒了。非常感谢您的帮助。

您需要将类
Integer
放在括号中:

unless @response.is_a?(Integer) && @response.to_str.length == 4

您实际上是在
(Integer&&@response.to_str.length==4)
上计算
is_a?
,这是一个布尔值,不是一个类或模块。

如果我没有特别将类放在括号中,我没有意识到它包含了整个条件的后半部分。非常感谢。由于某种原因,我仍然无法让它接受长度为四个数字的东西,但每次只能有一个问题。@MarkHJr,如果
@response
确实是一个
整数
,那么你就不能对它调用
到str
,只能
到s
。当我在控制台中使用值
454524
尝试您的测试时,我收到以下消息:
NoMethodError:undefined method
to_str'for 454524:Fixnum`。回答和解释很好!很快就解决了我的问题,而这可能需要更长的时间+1.