IO.readlines will not accept variable with file name Why?

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.

Thanks for any help.

--
Posted via http://www.ruby-forum.com/.

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. :slight_smile:

Kind regards

robert

···

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. 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.

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

I am not sure.

I think it is a simple typo in that

  filename1 (with a digit)

is asked by the get, but

  filename (without a digit)

is used in the call to IO.readlines.

IMHO, the code as shown above could never work with that typo ...

HTH,

Peter

···

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. 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. :slight_smile:

Robert Klemme wrote in post #1024202:

···

On Thu, Sep 29, 2011 at 11:14 AM, Joda jenson <jodajen2@yahoo.com> > wrote:

puts filearray [0]
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. :slight_smile:

Kind regards

robert

Robert I thought I had done that but the .inspect shows I am getting a
"\n" at the end of the string.

Thanks for the help on this sorry for the typo, it was really late when
I
was posting.

What is the easiest why to get the "\n" off the end?

--
Posted via http://www.ruby-forum.com/\.

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

Cheers

robert

···

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. 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. :slight_smile:

I am not sure.

I think it is a simple typo in that

filename1 (with a digit)

is asked by the get, but

filename (without a digit)

is used in the call to IO.readlines.

IMHO, the code as shown above could never work with that typo ...

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

Peter Vandenabeele wrote in post #1024204:

···

On Thu, Sep 29, 2011 at 11:56 AM, Robert Klemme > <shortcutter@googlemail.com>wrote:

> filearray = IO.readlines(filename)
>
Insert a line after "gets" like this:

I am not sure.

I think it is a simple typo in that

  filename1 (with a digit)

is asked by the get, but

  filename (without a digit)

is used in the call to IO.readlines.

IMHO, the code as shown above could never work with that typo ...

Yes, but if that were the case the error wouldn't be Errno::ENOENT.

The problem is the *content* of the filename1 variable, and that's what
Robert is trying to get the OP to check, using

p filename1

or equivalently:

puts filename1.inspect

--
Posted via http://www.ruby-forum.com/\.

Joda jenson wrote in post #1024263:

What is the easiest why to get the "\n" off the end?

You can use chomp() like:

filename1 = gets.chomp

and it'll get rid of the \n for you.

···

--
Posted via http://www.ruby-forum.com/\.

Look at chomp

peterv@ASUS:~$ irb
001:0> a = gets
test
=> "test\n"
002:0> a.chomp
=> "test"
003:0>

HTH,

Peter

···

On Thu, Sep 29, 2011 at 10:39 PM, Joda jenson <jodajen2@yahoo.com> wrote:

Robert Klemme wrote in post #1024202:
> On Thu, Sep 29, 2011 at 11:14 AM, Joda jenson <jodajen2@yahoo.com> > > wrote:
>> puts filearray [0]
>> 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. :slight_smile:
>
> Kind regards
>
> robert

Robert I thought I had done that but the .inspect shows I am getting a
"\n" at the end of the string.

Thanks for the help on this sorry for the typo, it was really late when
I
was posting.

What is the easiest why to get the "\n" off the end?

Ah thanks,

Peter

···

On Thu, Sep 29, 2011 at 2:09 PM, Brian Candler <b.candler@pobox.com> wrote:

Peter Vandenabeele wrote in post #1024204:
> On Thu, Sep 29, 2011 at 11:56 AM, Robert Klemme > > <shortcutter@googlemail.com>wrote:
>
>> > filearray = IO.readlines(filename)
>> >
>> Insert a line after "gets" like this:
>>
> I am not sure.
>
> I think it is a simple typo in that
>
> filename1 (with a digit)
>
> is asked by the get, but
>
> filename (without a digit)
>
> is used in the call to IO.readlines.
>
> IMHO, the code as shown above could never work with that typo ...

Yes, but if that were the case the error wouldn't be Errno::ENOENT.

Peter Vandenabeele wrote in post #1024266:

···

On Thu, Sep 29, 2011 at 10:39 PM, Joda jenson <jodajen2@yahoo.com> > wrote:

>>
> puts filearray [0]
Thanks for the help on this sorry for the typo, it was really late when
I
was posting.

What is the easiest why to get the "\n" off the end?

Look at chomp

peterv@ASUS:~$ irb
001:0> a = gets
test
=> "test\n"
002:0> a.chomp
=> "test"
003:0>

HTH,

Peter

Peter is there any difference between chomp and
This:
string = string.gsub(/\n/," ")
chomp looks easier to remember.
THanks both fixed the issue.
Thanks peter, roger and all fixed a headache for me.

--
Posted via http://www.ruby-forum.com/\.

Well yes, a lot of difference ...

gsub(/\n/, "") will produce a new string, where occurences of a newline
character ("\n") are replaced with a space.

chomp will remove the last newline/carriage return combination
(0 or 1 CR and 0 or 1 LF if I am right).

003:0> b = "\nabc\ndef\n\nghi\n\n\n"
=> "\nabc\ndef\n\nghi\n\n\n"
004:0> b.chomp
=> "\nabc\ndef\n\nghi\n\n"
005:0> b.gsub(/\n/," ")
=> " abc def ghi "
006:0> c = "abc\r"
=> "abc\r"
007:0> c.chomp
=> "abc"
008:0> d = "abc\r\n"
=> "abc\r\n"
009:0> d.chomp
=> "abc"
010:0> d.gsub(/\n/," ")
=> "abc\r "
peterv@ASUS:~$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]

E.g. googling for "ruby chomp documentation" led me to this entry:

    class String - RDoc Documentation

For understanding regular expressions exactly, that needs a little
more study (but very rewarding).

Use irb (or pry) to test for yourself what the different behaviors are,
that's how you will really understand how it works.

HTH,

Peter

···

On Thu, Sep 29, 2011 at 11:20 PM, Joda jenson <jodajen2@yahoo.com> wrote:

Peter Vandenabeele wrote in post #1024266:
> On Thu, Sep 29, 2011 at 10:39 PM, Joda jenson <jodajen2@yahoo.com> > > wrote:
>
>> >>
>> > puts filearray [0]
>> Thanks for the help on this sorry for the typo, it was really late when
>> I
>> was posting.
>>
>> What is the easiest why to get the "\n" off the end?
>>
>
> Look at chomp
>
> peterv@ASUS:~$ irb
> 001:0> a = gets
> test
> => "test\n"
> 002:0> a.chomp
> => "test"
> 003:0>
>
> HTH,
>
> Peter

Peter is there any difference between chomp and
This:
string = string.gsub(/\n/," ")
chomp looks easier to remember.

Peter Vandenabeele wrote in post #1024278:

Peter is there any difference between chomp and
This:
string = string.gsub(/\n/," ")
chomp looks easier to remember.

Well yes, a lot of difference ...

gsub(/\n/, "") will produce a new string, where occurences of a newline
character ("\n") are replaced with a space.

chomp will remove the last newline/carriage return combination
(0 or 1 CR and 0 or 1 LF if I am right).

chomp does also produce a new string:

irb(main):010:0> a = "abc\n"
=> "abc\n"
irb(main):011:0> a.chomp
=> "abc"
irb(main):012:0> a
=> "abc\n"

But I think chomp is roughly equivalent to
    gsub(/\r?\n?\z/, '')

···

--
Posted via http://www.ruby-forum.com/\.

Indeed, thanks for pointing that out.

Peter

···

On Fri, Sep 30, 2011 at 4:01 PM, Brian Candler <b.candler@pobox.com> wrote:

Peter Vandenabeele wrote in post #1024278:

>> Peter is there any difference between chomp and
>> This:
>> string = string.gsub(/\n/," ")
>> chomp looks easier to remember.
>
> Well yes, a lot of difference ...
>
> gsub(/\n/, "") will produce a new string, where occurences of a newline
> character ("\n") are replaced with a space.
>
> chomp will remove the last newline/carriage return combination
> (0 or 1 CR and 0 or 1 LF if I am right).

chomp does also produce a new string: