Why is gets throwing an error when arguments are passed to my ruby script?
Ruby will automatically treat unparsed arguments as filenames, then open and read the files making the input available to ARGF
($<
). By default, gets
reads from ARGF. To bypass that:
$stdin.gets
It has been suggested that you could use STDIN
instead of $stdin
, but it's usually better to use $stdin
.
Additionally, after you capture the input you want from ARGV
, you can use:
ARGV.clear
Then you'll be free to gets
without it reading from files you may not have intended to read.
Ruby gets method throws an exception when arguments are passed from the console
gets
reads from stdin if no arguments are passed, and from the file that was passed as an argument otherwise. You are passing an argument true
, ergo gets
tries to read from a file named true
, which apparently doesn't exist.
This is the very first sentence of the documentation of gets
:
Returns (and assigns to
$_
) the next line from the list of files inARGV
(or$*
)
Using gets() gives No such file or directory error when I pass arguments to my script
It looks like you want to the user to type some input by reading a line from STDIN
, the best way to do this is by calling STDIN.gets
and not gets
. So your line becomes:
word = STDIN.gets.chomp
This is documented as IO.gets
. STDIN
is an instance of IO
.
Right now, you're executing Kernel.gets
, which does something different (emphasis mine):
Returns (and assigns to $_) the next line from the list of files in ARGV (or $*), or from standard input if no files are present on the command line.
This appears to behave like STDIN.gets
if ARGV
is empty, but is not the same thing, hence the confusion.
Am unable to combine gets.to_i with ARGV arguments, in Ruby
ARGV
looks like an array, but it doesn't behave exactly like one. You won't have access to the second argument, unless you remove the first one first. Your code works if you rewrite it like this:
a, b, c, d, e, f = (0..5).map { ARGV.shift }
puts "you will first go to point #{a}, then #{b}, then #{f}, finishing off with #{e} and finally #{d}."
print "Give me a number: "
time = gets.to_i
puts "you can start at #{time}"
Get original type of an argument passed to ruby script
No, arguments passed into scripts are always strings.
Builder throwing wrong number of arguments error when passed a block in Ruby 1.9
That is actually because in ruby 1.9, lambda and proc behave subtly differently. Think of lambda, being mathematically precise, requiring the exact number of arguments specified, while proc exhibits the more permissive behavior of ruby 1.8. For example,
a = lambda {|v| p v }
a.call() # ArgumentError: wrong number of arguments (0 for 1)
a.call(1) # prints "1"
a.call(1, 2) # ArgumentError: wrong number of arguments (2 for 1)
b = proc {|v| p v }
b.call() # prints "nil"
b.call(1) # prints "1"
b.call(1, 2) # prints "1"
Note that both objects are of type Proc, but can be distinguished from each other by the .lambda? method.
a.class # => Proc
a.lambda? # => true
a.arity # => 1 (number of parameters)
b.class # => Proc
b.lambda? # => false
b.arity # => 1 (number of parameters)
Related Topics
Ruby Ssl Error - Sslv3 Alert Unexpected Message
Windows: Rails: Error Installing Bson_Ext
Editing Existing Rails Migrations Is a Good Idea
How to Test If Parameters Exist in Rails
How to Implement Cookie Support in Ruby Net/Http
Name of This Month (Date.Today.Month as Name)
Descending Sort by Value of a Hash in Ruby
Allow Public Connections to Local Ruby on Rails Development Server
Trying to Install Ruby-Filemagic on Snow Leopard Using Brew Rather Than Ports
Throw Exception When Re-Assigning a Constant in Ruby
Elegant Chained 'Or's for Tests on Same Variable in Ruby
How to Elegantly Rename All Keys in a Hash in Ruby
To Change Directory Inside a Ruby Script
How to Generate Links with Trailing Slash in Rails 3
In Ruby, Is There No Way to Dynamically Define a Local Variable in the Current Context