From: Robert Klemme Date: 2011-09-29T18:56:46+09:00 Subject: Re: IO.readlines will not accept variable with file name Why? On Thu, Sep 29, 2011 at 11:14 AM, Joda jenson wrote: > I am fairly new to Ruby and I am stuck on this.  Would someone have a > suggestion. > > I'm trying to read a file with IO.readlines, letting a user give the > file name. > __________________________________________________ > puts "what is the name of the file=>" > filename1 = gets > > filearray = IO.readlines(filename) > puts filearray [0] > __________________________________________________ > > If I hard code the file name works great. > > filearray = IO.readlines("/home/user/file.txt") > > __________________________________________________ > It seems to be something to do with the double quotes. > I have tried to wrap the variable with double quotes. > > filename1 = ("\"/home/user/file.txt\"") > > It will print the Double quote but still will not work if I pass it to > the IO.readlines() > > It gives a (Errno::ENOENT) > > It doesn't seem to matter how I doctor a string with quotes I can't get > IO.readlines() to accept it.  Only hard coding it works for some reason. Insert a line after "gets" like this: puts "what is the name of the file=>" filename1 = gets p filename1 # look at the output filearray = IO.readlines(filename) puts filearray [0] You'll immediately see what's wrong. :-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/