0

I am writing a program in Ruby, but I'm having trouble getting information from the command prompt using the sciTE text editor. For example, when I attempt to run code that requires input from the user (e.g., puts "Please enter your name: " name = gets()), the command line pops up but the information that I "put" to the screen does not show. When I try to enter information (letters or numbers), nothing happens. I then close out the command prompt and receive an error code in the sciTE output window. Any thoughts? Thanks!

2 Answers2

0

I encountered this problem today going through Mr. Neighborly's Ruby tutorial, and worked out the answer. It is a known problem with SciTE and the way it interacts with stdin (gets()) on Windows.

To run the tutorial code as-is, use the command prompt to run your program, rather than the SciTE 'Go' key [F5] mentioned in Mr. Neighborly's Humble Little Ruby Book, as follows:

Workaround 1:

  1. Your Windows Ruby installation should include a shortcut (under the Start menu) called "Start Command Prompt with Ruby". Run that.
  2. In the Ruby command prompt, navigate to the path where your Hello World Ruby file is located.
  3. Type ruby hello.rb and press [Enter]. (Replace hello.rb with your file name.)

Workaround 2:

Another alternative is to ignore the broken command prompt, and use SciTE's internal one instead. But, this requires you to add $stdout.flush() after each puts() statement, like this:

puts "Hello, world. What is your name?"  
$stdout.flush()  # Add this line!  
myname = gets()  
puts "Well, hello there " + myname + "."  
$stdout.flush()  # Add this line!  

Note

The next inconsistency in Chapter 0 of the tutorial, which you will probably notice immediately, is that the newline (\n) character is included in your myname variable (the input from gets()). You will probably see the following output (note the "." on the second line):

Well, hello there Yournamehere  
.  

To fix this, change myname.gets() to myname.gets().chomp(). (Feel free to look up chomp() in the online Ruby documentation.)

Tamara
  • 840
  • 7
  • 12
0
I do hope I understand what you want to do, but this is a sample here from my irb prompt:

1.9.2-p290 :001 > def a
1.9.2-p290 :002?>   puts "what is your name"
1.9.2-p290 :003?>   name = gets.chomp
1.9.2-p290 :004?>   puts "My name is #{name}"
1.9.2-p290 :005?>   end
 => nil 
1.9.2-p290 :006 > a
what is your name
abbb
My name is abbb
 => nil 
1.9.2-p290 :007 > 
bjhaid
  • 9,592
  • 2
  • 37
  • 47