Forum: Ruby undefined method `empty?' for nil:NilClass (NoMethodError)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D4f5ba376e2d4e280889ed462939f632?d=identicon&s=25 anon1m0us (Guest)
on 2007-02-06 20:16
(Received via mailing list)
Hi;
I have a script that goes through a file to match information and puts
them in different arrays. When I run the program I receive in the
following error:  undefined method `empty?' for nil:NilClass
(NoMethodError).


My code is:
Cemployee=Array.new
CemployeeID=Array.new
CemployeeName=Array.new
CemployeeGroup=Array.new
CMachineGroup=Array.new
CMachineShares=Array.new
CShareAccess=Array.new

counter=0
excelcounter1=2
excelcounter2=2
excelcounter3=2
excelcounter4=2
File.open("D:\\Reports.txt").each { |lineinfo|
  user=lineinfo
  group=lineinfo
  machine=lineinfo
  shares=lineinfo
  access=lineinfo

  m = /^([C]\d+)\s(\w+.+)/.match(user)
  n= /^\b(Groups:)\s.+/.match(group)
  o= /^\b(Machine Group:)\s(\w+.)*/.match(machine)

  p=/^\b(Share Name)\s.*/.match(shares)
  q=/^\b(Access Level)\s.*/.match(access)

  if m
    CemployeeID[counter],CemployeeName[counter]=user.split(" ")
    counter+=1
    excelcounter1+=1

  elsif p
    junk,CMachineShares[counter]=group.split(": ")
    CMachineShares[counter].empty?
    CMachineShares[counter].slice!("\n")
    puts CMachineShares[counter]
    puts CMachineShares[counter]
    #$excel.Worksheets(2).Range("A#{excelcounter4}")
['Value']=CMachineShares[counter]
    #excel.Worksheets(2).Columns.AutoFit
    #counter+=1
  end

}
D4f5ba376e2d4e280889ed462939f632?d=identicon&s=25 anon1m0us (Guest)
on 2007-02-06 20:26
(Received via mailing list)
The output fails when it comes to nil.
Here is a sample output:

\\Server1v1\BACKUP
nil
nil
\\Server4v1\BACKUP
\\Server4v1\BACKUP2
D4f5ba376e2d4e280889ed462939f632?d=identicon&s=25 anon1m0us (Guest)
on 2007-02-06 20:31
(Received via mailing list)
I figured the answer:

junk,CMachineShares[counter]=group.split(": ")
    CMachineShares[counter]=CMachineShares[counter].to_a
    #CMachineShares[counter].slice!("\n")
    CMachineShares[counter].delete_if {|x| x.empty?}
    puts CMachineShares[counter]
This topic is locked and can not be replied to.