Getting an Int value from keyboard

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