From: Peter Vandenabeele Date: 2011-09-30T07:32:37+09:00 Subject: Re: IO.readlines will not accept variable with file name Why? --0016369fa10ac4db1404ae1c157c Content-Type: text/plain; charset=UTF-8 On Thu, Sep 29, 2011 at 11:20 PM, Joda jenson wrote: > Peter Vandenabeele wrote in post #1024266: > > On Thu, Sep 29, 2011 at 10:39 PM, Joda jenson > > 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. > 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: 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 --0016369fa10ac4db1404ae1c157c--