Forum: Ruby Error when reading file from command prompt

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cf63da956b6ba955687a2f2f262928cb?d=identicon&s=25 Mmcolli00 Mom (mmcolli00)
on 2009-04-22 18:02
Hi. I have a program that is supposed to get a filename at the command
prompt. It reads in the input ok but doesn't initialize to the file. I
have more code that takes the file and parses it but I did not paste
that below.

Question: Is there a step that I am missing in f = File.new(myinput)?
Thanks MC

inputFile = print("Enter Filename: ")
$stdout.flush
input = gets
myinput = input.to_s

f = File.new(myinput) <-- in 'initialize': Invalid argument - filename
(Errno::EINAL) from crb.rb in 'new'
729474a7121e36e94591b3d6e2611077?d=identicon&s=25 Suresh Kk (suresh7)
on 2009-04-22 18:10
Mmcolli00 Mom wrote:
> Hi. I have a program that is supposed to get a filename at the command
> prompt. It reads in the input ok but doesn't initialize to the file. I
> have more code that takes the file and parses it but I did not paste
> that below.
>
> Question: Is there a step that I am missing in f = File.new(myinput)?
> Thanks MC
>
> inputFile = print("Enter Filename: ")
> $stdout.flush
> input = gets
> myinput = input.to_s
>
> f = File.new(myinput) <-- in 'initialize': Invalid argument - filename
> (Errno::EINAL) from crb.rb in 'new'

input = gets

input = gets.chomp
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2009-04-22 18:12
(Received via mailing list)
On Wed, Apr 22, 2009 at 6:02 PM, Mmcolli00 Mom <mmc_collins@yahoo.com>
wrote:
> input = gets
Try:

input = gets.chomp

gets is returning the \n at the end of the string.

> myinput = input.to_s
>
> f = File.new(myinput) <-- in 'initialize': Invalid argument - filename
> (Errno::EINAL) from crb.rb in 'new'

Jesus.
Ac816d6a1c7a649d7aafed6afd05f64f?d=identicon&s=25 Alan Claughan (Guest)
on 2009-04-22 21:50
(Received via mailing list)
Wow you seem to be a little confused her

in line 1 you try to get a inputFile name from a print statement.

You actually get the filename with line 3.

line 4 is unnecessary as gets returns a string anyway.

So you can do :

puts 'Enter Filename: '
$stdout.flush
filename = gets.chomp

inputfile = File.new(filename, 'w')

Alan.
Ac816d6a1c7a649d7aafed6afd05f64f?d=identicon&s=25 Alan Claughan (Guest)
on 2009-04-22 21:54
(Received via mailing list)
Oops that should be :

puts 'Enter Filename: '
$stdout.flush
filename = gets.chomp

inputfile = File.new(filename, 'r')

:-)
Cf63da956b6ba955687a2f2f262928cb?d=identicon&s=25 Mmcolli00 Mom (mmcolli00)
on 2009-04-22 23:02
Alan Claughan wrote:
> Oops that should be :
>
> puts 'Enter Filename: '
> $stdout.flush
> filename = gets.chomp
>
> inputfile = File.new(filename, 'r')
>
> :-)

thanks!
This topic is locked and can not be replied to.