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.
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
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?
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:
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.
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.