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

Actually inspect didn’t work. I had a typo :), please help

Thanks for all comments.

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

Thanks, that worked.

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

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

Thanks, that worked.

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

There’s also a method call syntax for that:

if string.match("\n")

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

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

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$/

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 –

If you want to test whether a string contains only a newline, you can
use ==:

strings = [“hello\n”, “\n”]

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

–output:–
yes -->
<–

Note that double quotes are required around the string “\n” for ruby to
interpret the ‘’ and ‘n’ combination as a newline.

And if what you are really after is to remove a newline at the end of a
string, you can use chomp():

strings = [“hello\n”, “hello”]

strings.each do |str|
puts “–> #{str.chomp} <–”
end

–output:–
–> hello <–
–> hello <–

On Fri, Mar 25, 2011 at 9:28 PM, Damir S. [email protected] wrote:

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

Kind regards

robert

On Mon, Mar 28, 2011 at 1:07 PM, Robert K.
[email protected] wrote:

Code that didn’t work:

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

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

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.

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