Ruby's Argv Can Be Empty on Windows Depending on a Way to Run Script

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.

Script to run against stdin if no arg; otherwise input file =ARGV[0]

You can eliminate the first five lines entirely.

From Pickaxe

$<: An object that provides access to
the concatenation of the contents of
all the files given as command-line
arguments or $stdin (in the case where
there are no arguments). $< supports
methods similar to a File object:
binmode, close, closed?, each,
each_byte, each_line, eof, eof?, file,
filename, fileno, getc, gets, lineno,
lineno=, path, pos, pos=, read,
readchar, readline, readlines, rewind,
seek, skip, tell, to_a, to_i, to_io,
to_s, along with the methods in
Enumerable. The method file returns a
File object for the file currently
being read. This may change as $<
reads through the files on the command
line. [r/o]

Therefore:

print $<.read

Kernel.gets is shorthand for $<.gets, so:

while s = gets
puts s
end

How do you pass the string *.* to ruby as a command line parameter?

You may need to put this into literal quotes:

test_argv "*.*"

The quotes should avoid having the command-line arguments get expanded on you prematurely.

How to pass parameters to ruby command line script

Like many other Unix utilities, ruby recognizes - as a special filename for STDIN:

$ echo "p ARGV" | ruby - foo bar
["foo", "bar"]

or:

$ ruby - foo bar << EE
> p ARGV
> EE
["foo", "bar"]

or: (pressing controlD to end the input)

$ ruby - foo bar
p ARGV
["foo", "bar"]

Running a selfwritten ruby program outside of an IDE

The simple answer that should work for all versions of Windows is to just create a simple batch launcher.

Create a .bat file. I usually just create a new .txt file via "right click > new > text document". Then rename it, highlight everything, including the extension, and rename it to something like run.bat. The .bat part is important. Once you rename it, the icon should change to gears. If you can't overwrite the extension, or Windows is still treating it as a text document, you'll need to either manually save it as a bat, or disable "hide file extensions" in the explorer settings so the extension can be changed.


Edit the bat file, and put into it something like:

@echo off

YOUR RUN COMMAND HERE THAT YOU WOULD NORMALLY TYPE MANUALLY

pause

Paste the command that you would normally run manually where the capital text is. The first line is so it doesn't repeat the commands back, and the pause is so if an error happens, the command prompt doesn't immediately close. This gives you a chance to read the error.


Save it and close it. Now, if you double click on the bat file, your program should run.

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 in ARGV (or $*)

Running a Ruby Program as a Windows Service?

Check out the following library: Win32Utils. You can create a simple service that you can start/stop/restart at your leisure. I'm currently using it to manage a Mongrel instance for a Windows hosted Rails app and it works flawlessly.



Related Topics



Leave a reply



Submit