On Thu, Sep 29, 2011 at 12:07 PM, Peter Vandenabeele
<peter / vandenabeele.com> wrote:
> On Thu, Sep 29, 2011 at 11:56 AM, Robert Klemme
> <shortcutter / googlemail.com>wrote:
>
>> On Thu, Sep 29, 2011 at 11:14 AM, Joda jenson <jodajen2 / yahoo.com> wrote=
:
>> > I am fairly new to Ruby and I am stuck on this. =A0Would 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=3D>"
>> > filename1 =3D gets
>> >
>> > filearray =3D IO.readlines(filename)
>> > puts filearray [0]
>> > __________________________________________________
>> >
>> > If I hard code the file name works great.
>> >
>> > filearray =3D 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 =3D ("\"/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 ge=
t
>> > IO.readlines() to accept it. =A0Only hard coding it works for some rea=
son.
>>
>> Insert a line after "gets" like this:
>>
>> puts "what is the name of the file=3D>"
>> filename1 =3D gets
>> p filename1 # look at the output
>>
>> filearray =3D IO.readlines(filename)
>> puts filearray [0]
>>
>> You'll immediately see what's wrong. :-)
>>
>>
> I am not sure.
>
> I think it is a simple typo in that
>
> =A0filename1 =A0(with a digit)
>
> is asked by the get, but
>
> =A0filename (without a digit)
>
> is used in the call to IO.readlines.
>
> IMHO, the code as shown above could never work with that typo ...

Right, but you might notice that I just copied the code from OP. :-)

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/