How to "find" new lines

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.

···

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

I assume that txtArray is a String in spite of the name. If so, then your
comparison is probably easiest done through regular expressions:

if txtArray =~ /\n/
  ...
end

···

On Fri, Mar 25, 2011 at 9:28 PM, Damir Sigur <damir@haakon.com> 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.

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

Thanks, that worked.

I am on the way of learning more, and there is so much to learn!

···

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

Actually, that solution does not work, as you can see here:

strings = ["hello\n", "\n"]

strings.each do |str|
  if str =~ /\n/
    puts "yes -- #{str}"
  end
end

--output:--
yes -- hello
yes --

You can use == to test whether a string contains only a newline:

strings = ["hello\n", "\n"]

strings.each do |str|
  if str == "\n"
    puts "yes -->#{str}<--"
  end
end

--output:--
yes -->
<--

···

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

And if you want to replace all the newlines in some text, you can use
gsub():

str =<<ENDOFSTRING
hello world
goodbye
hi again world
ENDOFSTRING

result = str.gsub(/\n/, ' ')
puts result

--output:--
hello world goodbye hi again world

···

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

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

Kind regards

robert

···

On Fri, Mar 25, 2011 at 9:28 PM, Damir Sigur <damir@haakon.com> 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.

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

don't know if this will help you but ruby 1.9 has the each_line method.

example:

def line_count( s)
  c = 0
  s.each_line { |l| c+=1 }
  return( c)
end

line_count( "I am \na multi\nline\nstring!")

=> 4

if you want to test against the ascii equivalent in 1.8.7 it's ?\n and
in 1.9.2 it's "\n".ord which should return 10 in decimal format.

···

On Fri, Mar 25, 2011 at 3:28 PM, Damir Sigur <damir@haakon.com> 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.

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

There's also a method call syntax for that:

    if string.match("\n")

. . . in case you find circumstances where you'd prefer that approach.

···

On Sat, Mar 26, 2011 at 06:10:37AM +0900, Damir Sigur wrote:

Thanks, that worked.

I am on the way of learning more, and there is so much to learn!

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

7stud is right. I missed the important word "only" in your original email.
My original method will produce false positives if you are indeed
interested in finding strings which are only "\n".

The "==" method works fine. Regexp is also possible (though probably
slower) if done as:

if str =~ /^\n$/

···

On Sat, Mar 26, 2011 at 3:43 AM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

And if you want to replace all the newlines in some text, you can use
gsub():

str =<<ENDOFSTRING
hello world
goodbye
hi again world
ENDOFSTRING

result = str.gsub(/\n/, ' ')
puts result

--output:--
hello world goodbye hi again world

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

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

···

On Mon, Mar 28, 2011 at 1:07 PM, Robert Klemme <shortcutter@googlemail.com> wrote:

On Fri, Mar 25, 2011 at 9:28 PM, Damir Sigur <damir@haakon.com> 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

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

Damir, to know if a string is exactly a newline, please simply use ==:

   if str == "\n"
     ...
   end

There are indirect ways to get the same result, you can also go from
Barcelona to Paris via New York, but a straight line is nicer.

Please people stop suggesting regexps for this simple problem.

Chris Kottom wrote in post #989369:

7stud is right. I missed the important word "only" in your original
email.
My original method will produce false positives if you are indeed
interested in finding strings which are only "\n".

The "==" method works fine. Regexp is also possible (though probably
slower) if done as:

if str =~ /^\n$/

Wrong - that doesn't match only strings containing just a newline.

str = "\n"

=> "\n"

str =~ /^\n$/

=> 0

str = "\n\n"

=> "\n\n"

str =~ /^\n$/

=> 0

str = "\n\nabc"

=> "\n\nabc"

str =~ /^\n$/

=> 0

With a regexp, you need str =~ /\A\n\z/

str = "\n"

=> "\n"

str =~ /\A\n\z/

=> 0

str = "\n\n"

=> "\n\n"

str =~ /\A\n\z/

=> nil

str = "\n\nabc"

=> "\n\nabc"

str =~ /\A\n\z/

=> nil

(beware: this is an area where Ruby's regexps are *not* the same as
Perl's regexps)

···

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