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.

On Thu, Sep 29, 2011 at 11:14 AM, Joda jenson [email protected]
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

On Thu, Sep 29, 2011 at 11:56 AM, Robert K.
removed_e[email protected]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 …

HTH,

Peter

Peter V. wrote in post #1024204:

On Thu, Sep 29, 2011 at 11:56 AM, Robert K.
[email protected]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

On Thu, Sep 29, 2011 at 12:07 PM, Peter V.
[email protected] wrote:

puts “what is the name of the file=>”


It doesn’t seem to matter how I doctor a string with quotes I can’t get

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

Cheers

robert

On Thu, Sep 29, 2011 at 2:09 PM, Brian C. [email protected]
wrote:

I think it is a simple typo in that

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

Ah thanks,

Peter

Robert K. wrote in post #1024202:

On Thu, Sep 29, 2011 at 11:14 AM, Joda jenson [email protected]
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?

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.

On Thu, Sep 29, 2011 at 10:39 PM, Joda jenson [email protected]
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

[email protected]:~$ irb
001:0> a = gets
test
=> “test\n”
002:0> a.chomp
=> “test”
003:0>

HTH,

Peter

Peter V. wrote in post #1024266:

On Thu, Sep 29, 2011 at 10:39 PM, Joda jenson [email protected]
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

[email protected]:~$ 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.

On Thu, Sep 29, 2011 at 11:20 PM, Joda jenson [email protected]
wrote:

What is the easiest why to get the “\n” off the end?
003:0>

HTH,

Peter

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

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 "
[email protected]:~$ 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:

http://ruby-doc.org/core/classes/String.html#M001188

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 Fri, Sep 30, 2011 at 4:01 PM, Brian C. [email protected]
wrote:

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:

Indeed, thanks for pointing that out.

Peter

Peter V. 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/, ‘’)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs