0

So I'm using atom to code up a calculator and I can't seem to get the console to output the first instance of puts. It just says [Finished in 2.88s]. Is there anything missing that's causing my code to not be printed to the console or am I just stupid to think that it should be outputting anything to the console? Here is my code:


def add
  puts "What is the first number to be added?"
  n1 = gets.chomp
  puts "What is the second number to be added?"
  n2 = gets.chomp
  answer = n1 + n2
  puts "Congrats, your number is #{answer}"
end
def sub
  puts "What is the first number to be subtracted?"
  n1 = gets.chomp
  puts "What is the second number to be subtracted?"
  n2 = gets.chomp
  answer = n1 - n2
  puts "Congrats, your number is #{answer}"
end
def multiply
  puts "What is the first number to be multiplied?"
  n1 = gets.chomp
  puts "What is the second number to be multiplied?"
  n2 = gets.chomp
  answer = n1 * n2
  puts "Congrats, your number is #{answer}"
end
def divide
  puts "What is the first number to be divided?"
  n1 = gets.chomp
  puts "What is the second number to be divided?"
  n2 = gets.chomp
  answer = n1 / n2
  puts "Congrats, your number is #{answer}"
end

print "Would you like to add, subtract, multiply, or divide a number? "
response = gets.chomp
if response == "add" then
  add
elsif response == "subtract" then
  sub
elsif response == "multiply" then
  multiply
else response == "divide"
  divide
end
  • It seems to be an atom configuration issue. By the way, you should cast yours n1 and n2 as integer with "n1.to_i" otherwith you will have a concatenation instead of an addition (1 + 2 will give 12 as "1" + "2") – GPif Aug 21 '20 at 14:59
  • Just added the .to_i to my n1's and n2's but how would I go about fixing my configuration issue? – sleepbud Aug 21 '20 at 15:08
  • I am not an atom user. But you can test your code either by saving it in a .rb file and run it in a console via ruby [your file].rb, either in a console run irb and copy past your code. – GPif Aug 21 '20 at 15:16
  • How would I do that? I'm sorta new to ruby and have only been using ruby on atom. – sleepbud Aug 21 '20 at 15:23
  • Depends on your os. You have to open a terminal first (run the program terminal on macos or cmd on windows). If your ruby is installed properly, just type irb and then you can copy past ruby code. – GPif Aug 21 '20 at 15:30
  • To check if output to console works, just use this program: `puts "sometthing to console"`. From console launch the program (https://stackoverflow.com/questions/8721369/how-to-execute-a-ruby-script-in-terminal) or use IRB (https://stackoverflow.com/questions/tagged/irb?tab=Votes). – iGian Aug 21 '20 at 15:36
  • No, it should work. Try it on the repl.it site by creating a ruby repl. You can learn there or download VS code if you can't seem to figure it out. – yusrasyed Aug 23 '20 at 10:42

0 Answers0