This is a very silly question, but it doesn't work for me.
I am trying to make the program wait for my input. I tried replacing gets with stdin.gets, and $stdin.gets and when I try gets.chomp I get a nil class exception.
puts "Get works here?"
option = gets
puts option
To work this,you need to call your .rb file from your command prompt. Like say you save your code in a file called test.rb.
test.rb
puts "Get works here?"
option = gets
puts option
Then run from your command prompt:
C:\Users\arup> ruby test.rb
my script was also not waiting for input from gets(), but started to do so when I used
$stdin.gets("\n")
Related
I have this Ruby program that I created under Windows 7. At several points through it I need to create a couple of files, so I use the following bit of code:
File.open('my_file.txt', "w") {|file| file.puts 'the stuff I want to put inside' }
It works pretty well if I launch the program by double clicking on it, but it doesn't work if I launch it through the command prompt.
I get no error, though. The program runs normally. It just seems to skip the File.open method, and thus it doesn't create any file, and then it keeps going through the rest of the program. I made sure of it by creating a loop of file creation together with the line:
puts "whatever"
The loop is running correctly: I get "whatever" printed on screen thousands of times per second as expected, but still no file is created.
Now I need to tell you I've had this problem for weeks and never found a solution to it but it didn't bother me too much either since everything was working normally when I simply launched the program by double clicking on it instead of launching it through the command prompt.
However I now need to use it on Raspbian (on a Raspberry Pi Zero W), and I have the same issue when launching it through the terminal, but I know no other way to launch a Ruby program on Linux.
Here is the full code as required:
require 'io/console'
loop do
hidden_data = '{"hidden_data":"true"}'
File.open('hidden_data.json', "w") {|file| file.puts hidden_data }
puts "Insert your password:"
password = STDIN.noecho(&:gets).chomp
if password == "1234"
hidden_data = '{"hidden_data":"false"}'
File.open('hidden_data.json', "w") {|file| file.puts hidden_data }
puts "Access granted"
sleep 60
else
puts "Wrong password"
end
end
When running a ruby script with command line arguments, the "gets" is no longer blocking, it doesn't work.
test.rb
#!/usr/bin/ruby
puts "should wait for input"
gets
puts "test"
and here is how I run it
$ ./test.rb test.rb
should wait for input
test
It didn't wait.
I'm running Ubuntu 16.04 desktop, and Ruby from repository ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]
What am I doing wrong?
In addition to STDIN.gets like others have recommended, you can use plain gets if you call ARGV.clear beforehand. The regular gets works as expected if there aren't command like arguments to the script, but if there are, then it will read them. It's not really clear why you're using ./test.rb test.rb, but the second filename is a command line argument.
More specifically, if regular gets is called when ARGV is populated, then the result will be the contents of the file.
max#max ~> echo "content" > test.txt
max#max ~> ruby -e "puts ARGV.inspect; puts gets" test.txt
["test.txt"]
content
Nevermind,
The "gets" actually takes the first line from the file I added in the cli arguments.
Very weird.
I wrote a Ruby script like the following example. The basic functionality is the same:
# get input from the user
input = gets.chomp
# do awesome stuf with this input and print the response
puts do_awesome_stuff(input)
The problem is when I run the script it prints the solution I want, but the console window closes right after. I want the console to keep open.
I'm currently on windows, but the solution should be working on every system.
One way is to run the ruby script with a .bat file and pause it, like so:
ruby script.rb
PAUSE
I hope there is a way without the additional .bat file. Does Ruby has a function like PASUE integrated?
It seems like you double click the ruby script file.
Instead issue the following command in cmd shell.
ruby filename.rb
If you don't want that, you can add gets to the end of the script.
# get input from the user
input = gets.chomp
# do awesome stuf with this input and print the response
puts do_awesome_stuff(input)
gets # <----
But this is not recommended because .. if you run the command in cmd shell or terminal you should type extra Enter to return to the shell.
Use the -r options of irb.
irb -r ./filename.rb
I'm trying to get simple user input in Ruby, but I can't get it working. I'm using the gets method, but the program never stops to ask me for input. I'm using Sublime Text 2 as my text editor, and I run the program in it, too (if this makes a difference).
Here's my code:
puts "What is your name?"
name = gets
puts "Hello " + name + ". How are you?"
And here's the error (and output) given to me:
C:/Users/Sten Sootla/Desktop/Ruby workspace/exercise.rb:3:in `+': can't convert nil into String (TypeError)
from C:/Users/Sten Sootla/Desktop/Ruby workspace/exercise.rb:3:in `'
What is your name?
[Finished in 0.1s with exit code 1]
Why doesn't the program stop to ask me for input?
Try using $stdin.gets instead of just a plain gets, this will force the input to come from stdin
Here's how I understand it. gets and puts are instance methods of IO, and the default IOs are $stdout and $stdin.
Calls to gets/puts will only be effective if the translator is capable of handling stdout/in e.g. IRB
If you run a ruby file from bash it works too.
io_test.rb
puts gets
in bash
ruby io_test.rb
Then it will "put" into stdout whatever it "gets" from stdin.
If you want to run code within ST2, check out the SublimeREPL plugin, available through Package Control. While you can use IRB, its main Ruby interface is through pry, which is a lot more powerful. You can use it as a classic REPL (think Clojure or LISP), and you can also transfer your code from one tab into the running REPL in another tab by selection, line range, or block.
Interestingly, your code above works in IRB for me, but not pry for some reason - it's reading my $EDITOR environment variable, which is set to subl -w but failing with Errno::ENOENT: No such file or directory - subl -w. Strange...
At any rate, I highly highly recommend SublimeREPL, as it's a really powerful tool, and is self-contained within ST2, so you don't have to keep flipping back and forth to your terminal, saving and reloading your programs.
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!
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 >
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:
Your Windows Ruby installation should include a shortcut (under the
Start menu) called "Start Command Prompt with Ruby". Run that.
In the Ruby command prompt, navigate to the path where your Hello
World Ruby file is located.
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.)