From: Peter Bailey Date: 2009-06-26T03:37:15+09:00 Subject: Re: How do I get an integer from an array? Brian Candler wrote: > Alex wrote: >> The index method of the array class is a method just like any other, so >> you >> could just as well write it like: >> >> foo.[](0).[](0) >> >> which calls the `[]' method on the result of the previous call of the >> `[]' >> method, which is an array. >> >> foo[0] is just a shortcut ruby gives you for calling foo.[](0) > > Or in this particular case, you can do > > foo.first.first Well, interestingly, I've succeeded in some of my scripts. But, in this one, it fails. It displays a few hundred filenames with page counts, but, in this directory, there are literally thousands of PDF files. So, it does a bunch, then dies. Dir.glob("*.pdf").each do |pdffile| pages = `pdfinfo #{pdffile}` pages = pages.scan(/^Pages:[ ]{2,99}([0-9]+)/) pages = pages[0][0].to_i puts "#{pdffile} #{pages}" end I get: ================ NoMethodError ===================== C:\Users\pb4072\Documents\scripts\RUBY\multitiffs.rb:8:in `block in
' pages = pages[0][0].to_i C:\Users\pb4072\Documents\scripts\RUBY\multitiffs.rb:5:in `each' Dir.glob("*.pdf").each do |pdffile| C:\Users\pb4072\Documents\scripts\RUBY\multitiffs.rb:5:in `
' Dir.glob("*.pdf").each do |pdffile| ============================================= Exception: undefined method `[]' for nil:NilClass -- Posted via http://www.ruby-forum.com/.