Checking for blank line

Hello,
I know this is an elementary problem, but I have been fooling around
with RoR for a little while and am now in need of doing some ruby
scripting. I am reading a text file line by line and I want to check if
the line is empty. For example:

file = File.new("test.txt", "r")
while (line = file.gets)
  @array = line.split(':')
    if @array[0] == 'cn'
      puts "#{@array[1]}"
    end
    if @array[0] == 'title'
      puts " Title: #{@array[1]}"
    end
    if @array[0] == 'company'
      puts " Company: #{@array[1]}"
    end
    if @array[0] == nil #~ THIS IS THE LINE I'M TALKING
ABOUT
      puts "-----"
    end
end
file.close

The input file just looks like this:

cn: John Smith
title: executive
company: ReallyCoolSoftware, inc.

cn: John Doe
title: janitor
company: NotSoCoolSoftware, inc.

etc...

How can I pick up those blank lines in my IF statement? I tried nil and
'', but neither seem to work.

Any help is appreciated!
Thank you!!
- Jeff

···

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

Jeff Miller wrote:

Hello,
I know this is an elementary problem, but I have been fooling around
with RoR for a little while and am now in need of doing some ruby
scripting. I am reading a text file line by line and I want to check if
the line is empty. For example:

file = File.new("test.txt", "r")
while (line = file.gets)
  @array = line.split(':')
    if @array[0] == 'cn'
      puts "#{@array[1]}"
    end
    if @array[0] == 'title'
      puts " Title: #{@array[1]}"
    end
    if @array[0] == 'company'
      puts " Company: #{@array[1]}"
    end
    if @array[0] == nil #~ THIS IS THE LINE I'M TALKING
ABOUT
      puts "-----"
    end
end
file.close

The input file just looks like this:

cn: John Smith
title: executive
company: ReallyCoolSoftware, inc.

cn: John Doe
title: janitor
company: NotSoCoolSoftware, inc.

etc...

How can I pick up those blank lines in my IF statement? I tried nil and
'', but neither seem to work.

Any help is appreciated!
Thank you!!
- Jeff
  
Remember, "blank" lines might not be truly empty:
irb(main):001:0> a = gets

=> "\n"
irb(main):002:0> a.split(":")
=> ["\n"]

Also, using split on an empty string is not going to give you nil, but an empty array:

irb(main):003:0> "".split(":")
=>

If you don't want the line endings in your strings, try using

while (line = file.gets.chomp)

or

while (line = file.gets.strip)

instead.

-Justin

if /\S/ !~ line # is blank

Thanks for the reply!

That does make sense! However, I still can't get this to work properly.
I tried to use strip and chomp, but both of them kicked me errors
"undefined method 'chomp' called for nil:NilClass (NoMethodError)". I
figure this is kind of odd, since I've used both of these in RoR before.
FYI, the code I posted is the entire script... perhaps I'm missing some
sort of 'require' statement or something?

Anyhow, I tried to use the \n, but I can't get to work either... I also
tried @array[0] == '\n' and a bunch of other little changes. How may I
construct an IF statement with this?

Thanks again,
- Jeff

···

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

Justin Collins wrote:

Jeff Miller wrote:

(...)>

Also, using split on an empty string is not going to give you nil, but
an empty array:

irb(main):003:0> "".split(":")
=>

Yes, but array[0] will return nil if array is empty, so the OP is doing
ok.

If you don't want the line endings in your strings, try using

while (line = file.gets.chomp)

This is too early, it will error when you get to the end of the file.
One line later is fine:

@array = line.chomp.split(':')

or

while (line = file.gets.strip)

instead.

-Justin

Regards,

Siep

···

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

Justin Collins wrote:

while (line = file.gets.strip)

This will fail at eof as then file.gets will return nil. The strip
should be put into the loop.

You can also heavily refactor:

file = File.new("test.txt", "r")
while (line = file.gets)
  line.strip!
  if line.empty?
    puts "-----"
  else
    @array = line.split(':')
    if m = @array[0].match(/(title|company)/)
      print " #{m[1].capitalize}: "
    end
    puts "#{@array[1]}"
  end
end
file.close

Also, since your input seems to have spaces after the colons, you may
want to ignore those. If so, replace the split with:

    @array = line.split(/\s*:\s*/)

···

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

Thanks guys! Using the regular expression worked!

···

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

Jeff Miller wrote:

Thanks for the reply!

That does make sense! However, I still can't get this to work properly. I tried to use strip and chomp, but both of them kicked me errors "undefined method 'chomp' called for nil:NilClass (NoMethodError)". I figure this is kind of odd, since I've used both of these in RoR before. FYI, the code I posted is the entire script... perhaps I'm missing some sort of 'require' statement or something?
  
Ah, good point. That happens on the last loop through, when the file is empty. My apologies.

Anyhow, I tried to use the \n, but I can't get to work either... I also tried @array[0] == '\n' and a bunch of other little changes. How may I construct an IF statement with this?

Thanks again,
- Jeff
  
Here's a way, with some other changes:

File.open("test.txt", "r") do |file|
  while (line = file.gets)
      array = line.split(':')
      if array[0] == 'cn'
        puts array[1]
      elsif array[0] == 'title'
        puts " Title: #{array[1]}"
      elsif array[0] == 'company'
        puts " Company: #{array[1]}"
      elsif line.strip.empty?
        puts "-----"
      end
  end
end

···

--------------

File.open do |f| ... end will make sure the file gets closed for you.
Using @array is not necessary, as it just needs to be a local variable, not an instance variable.
String interpolation is not necessary to print an element of the array.
line.strip will get rid of any whitespace and then you can check if the string is empty.
Also, note what happens when you get a line that is neither empty nor matches any of the above.
You can use a series of elsifs to make it slightly shorter/faster/easier to read.

-Justin

Might I suggest avoiding such control structures:

File.open("test.txt", "r").each_line do |line|
  array = line.strip.split(':')
  case array[0]
    when 'cn'
      puts array[1]
    when 'title'
      puts " Title: #{array[1]}"
    when 'company'
      puts " Company: #{array[1]}"
    else
      puts "-----" if line.strip.empty?
  end
end.close

This may or may not be ideal for you. You may also want to map the array
like .map {|e| e.strip}, since extra spaces may be included.

Arlen