2

I followed an example for this to start learning Ruby. The following code works inside of my text editor (Visual Studio Code). But when I open the file using the interactive ruby program. It doesn't seem to work.

puts "Hello there! Please enter a number to square: "
number = gets.chomp.to_i

def squared(number)
        squared = number * number
        return squared.to_s
end

puts "Your number is: " + number.to_s
puts "Your number squared is: " + square(number)

It prints out the initial "Hello there! Please enter a number to square:" But once I type a number and press enter, the program abruptly closes.

anothermh
  • 9,815
  • 3
  • 33
  • 52
Quinton
  • 23
  • 2
  • What command are you using to run it interactively? – Raj Dec 15 '18 at 00:46
  • Closes? How are you running it? – t56k Dec 15 '18 at 01:07
  • Well, I basically double clicked my example.rb file, it opened the command prompt and printed the "Please enter a number to square:" Sorry, I'm very new to Ruby. – Quinton Dec 15 '18 at 01:08
  • When I'm using the text editor I use this: ruby .\example.rb. Then it prints the question, I hit 2 or something, press enter, then it pushes out the correct number for me. But if it's running on the Interactive Ruby software I downloaded, it closes after the first attempt. – Quinton Dec 15 '18 at 01:09

1 Answers1

1

The problem is that you're double-clicking the example.rb file, which opens in cmd.exe and then closes automatically when it is done executing example.rb. That is the normal behavior for Windows: cmd.exe closes automatically after running whatever application invoked it.

What you want to do instead is manually open a command prompt (cmd.exe) and then run your application with ruby example.rb. This will keep cmd.exe open after your application finishes running, because cmd.exe was not invoked by your application; it was invoked manually by you.

This will allow you to see the Your number is: output after your application has finished running. This output is still being printed when you double-click example.rb, but the window closes so quickly that you can't see it.

Additionally, your application has some errors in it. Specifically, you're attempting to call square(number) when you should be calling squared(number).

Here is your code cleaned up a little bit:

puts 'Hello there! Please enter a number to square: '
number = gets.chomp.to_i

def squared(number)
  number * number
end

puts "Your number is: #{number}"
puts "Your number squared is: #{squared(number)}"

Note the use of single quotes for strings that do not include string interpolation, the implicit return for the squared method, and string interpolation for the puts calls.

anothermh
  • 9,815
  • 3
  • 33
  • 52
  • Thank you! I should have tried that. I quickly figured out that I had to type out the whole file path as well. Is there a quicker way to navigate through the cmd.exe? I guess I could copy paste the file path... – Quinton Dec 15 '18 at 01:45
  • I'll assume that your application is in `C:\Projects\example_app\example.rb` and that when you open `cmd.exe` you start in `C:\Windows\system32`. Run `cd \Projects\example_app`, then you can run `ruby example.rb`. If your app is on another drive then you have to switch to that drive first, e.g., `D:` then `cd \Projects\example_app`. – anothermh Dec 15 '18 at 01:51
  • Ah, I see now. Not sure why the example included to_i and to_s. Never seen those before. This version makes more sense and has less obvious information such as the squared = number * number. I appreciate your input. – Quinton Dec 15 '18 at 01:53
  • Oh! The problem was that I had it in the D: drive. And I didn't exactly choose an easy to find path. My pathing was Multimedia\Documents\GitHub\Rubybook\example.rb – Quinton Dec 15 '18 at 01:56
  • `to_i` is required because `gets` returns a string, and you need to coerce the string to an integer. That's what `to_i` does. `to_s` is used in your example because Ruby will not automatically coerce an integer to a string when called with `puts`, but string interpolation (description is linked above) will do that automatically. `puts 'foo' + 1.to_s` and `puts "foo#{1}"` are functionally equivalent, but "[the Ruby way](https://github.com/rubocop-hq/ruby-style-guide#strings)" says to use string interpolation, not string concatenation, where possible. – anothermh Dec 15 '18 at 01:57