__________________________________________________
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.
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
__________________________________________________
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.
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
Right, but you might notice that I just copied the code from OP.
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
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.
>
> 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?
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.
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.
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.
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).