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?
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?
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?
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?
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
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
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)