Ruby 'is_a?' class or module required (TypeError)

Mark H Jr picture Mark H Jr · Apr 29, 2014 · Viewed 9k times · Source

I'm playing around with Ruby and trying create a little bank account program. When I run this particular line of code which is running withing the create_account:

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

I get this response:

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>'

I can't exactly figure out what's going on, but I'm including the rest of my code. It's incomplete, obviously because I'm stuck on this part. I go through a while loop and enter 'Create Account' to start the create_account method. My goal is to run the conditional and require the user to type a 4 digit integer, like any ole pin number. If it is not an integer nor 4 digits in length, I call the create_account method to get them to start over.

I'm running ruby 2.0.0 if that information is necessary, but I wager it is likely more to do with my code. Stackoverflow is new to me, so I apologize if this question has been asked. I tried to do my homework as is advised before asking a question, but I'm still stumped. Any help is appreciated.

Answer

user740584 picture user740584 · Apr 29, 2014

You need to put the class Integer in parentheses:

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

You are actually evaulating is_a? on (Integer && @response.to_str.length == 4) which is a boolean value, not a class or module.