Getting an Int value from keyboard

Hi, I have this question. Is there any way to get an Int value from keyboard without getting a string and then turning it into Int?
I’ve been trying to do it but I only get this error

Terminal input is inherently a string, so I don’t think there’s a way to get around that. For this particular use case, it’s probably reasonable to do like gets.not_nil!.to_i. This approach is really only intended for if you are 100% sure it won’t be nil, but the compiler just can’t prove it.

More ideally you need to treat nil as a possible value an handle it accordingly with some conditional logic. E.g. if num1 is nil, then print an error and exit, or have the user try again.

See [Help] adding two numbers for a related issue.

3 Likes

Following up on @Blacksmoke16 's comment if you want to see the nil yourself, run your program after it compiles and hit Ctrl-D (End of Transmission / End of File character).

1 Like