From: Robert Klemme Date: 2011-03-28T20:12:22+09:00 Subject: Re: How to "find" new lines On Mon, Mar 28, 2011 at 1:07 PM, Robert Klemme wrote: > On Fri, Mar 25, 2011 at 9:28 PM, Damir Sigur wrote: >> I am new to ruby, and was trying to make a small code which would check >> if a string has only new line character in it. At the end I found that a >> method "inspect" will do the trick. >> >> Is that the only way to handle new line characters? >> >> How about other special characters, if there is a need to search for >> them in a text? >> >> Code that didn't work: >> >> if txtArray[i] <=> "\n" then >> >> Code that worked: >> >> if txtArray[i].inspect <=> "\n" then >> >> Thanks for all comments. > > If you want to remove the trailing newline, this may help as well: > > irb(main):005:0> s="foo\n" > => "foo\n" > irb(main):006:0> s.chomp! and puts "there was a newline!" > there was a newline! > => nil > irb(main):007:0> s > => "foo" > irb(main):008:0> s.chomp! and puts "there was a newline!" > => nil For the "only newline" check you could do irb(main):010:0> s="foo\n" => "foo\n" irb(main):011:0> s.chomp! == "" => false irb(main):012:0> s="\n" => "\n" irb(main):013:0> s.chomp! == "" => true Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/