0

I am trying to store an integer in x or y that gets returned as a string:

def add(a, b) puts "Adding #{a} + #{b}" a + b end def sub(a, b) puts "subtracting #{a} - #{b}" a - b end def mult(a, b) puts "multiplying #{a} * #{b}" a * b end def div(a, b) puts "dividing #{a} / #{b}" a / b end x = STDIN.gets.chomp y = STDIN.gets.chomp puts add(x,y) 

The response that I get from the program is:

23 43 Adding 23 + 43 2343 
1
  • 1
    The problem is that gets ONLY returns a string. It has no concept of anything but a string, nor does the console, which gets reads from by default. It's up to you to make that value be something else. Commented Feb 7, 2014 at 4:22

1 Answer 1

1

Convert it to an integer with to_i:

x = gets.to_i y = gets.to_i puts add(x,y) 

Note that there is no need to use STDIN.gets as long as you haven't passed any command line arguments, since gets reads from STDIN by default.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.