Forum: Ruby exception: can't convert nil into String

Posted by Peter Bailey (peterbailey)
on 2012-11-02 21:03
Hello,
I'm going nuts trying to figure out what's wrong with this script.
Here's the top part of my code, where it's failing:

require 'rubygems'
require 'fileutils'
require 'net/ftp'

Dir.chdir("F:/workflows/graphics/asura-post/vijay/in")
   if Dir.glob("*.pdf").length == 0 then
  exit
   end
pdffile = ARGV[0]
FileUtils.cp(pdffile, "//mako/archive/vijay")
FileUtils.mv(pdffile, "../scratch")

It's failing on the simple first FileUtils.cp line. It says "uncaught
exception: can't convert nil into String."

The script acts on the first file it sees in the network folder.

Any help would be appreciated.

Thanks a lot,
Peter
Posted by Bartosz Dziewoński (matmarex)
on 2012-11-02 21:40
(Received via mailing list)
You don't set the pdffile variable anywhere.

-- Matma Rex
Posted by Brian Candler (candlerb)
on 2012-11-02 22:16
Bartosz Dziewoński wrote in post #1082535:
> You don't set the pdffile variable anywhere.

?

He sets it to ARGV[0]. Perhaps he ran the script without providing an 
argument?

I have no idea why he's globbing *.pdf and then ignoring the results 
though.
Posted by Bartosz Dziewoński (matmarex)
on 2012-11-02 22:32
(Received via mailing list)
2012/11/2 Brian Candler <lists@ruby-forum.com>:
> Bartosz Dziewoński wrote in post #1082535:
>> You don't set the pdffile variable anywhere.
>
> ?
>
> He sets it to ARGV[0]. Perhaps he ran the script without providing an
> argument?

Ugh, that's right, sorry. I don't know why I missed it; perhaps I
should try reading more carefully :/

-- Matma Rex
Posted by Peter Bailey (peterbailey)
on 2012-11-05 14:01
Bartosz Dziewoński wrote in post #1082544:
> 2012/11/2 Brian Candler <lists@ruby-forum.com>:
>> Bartosz Dziewoński wrote in post #1082535:
>>> You don't set the pdffile variable anywhere.
>>
>> ?
>>
>> He sets it to ARGV[0]. Perhaps he ran the script without providing an
>> argument?
>
> Ugh, that's right, sorry. I don't know why I missed it; perhaps I
> should try reading more carefully :/
>
> -- Matma Rex

Thanks for your responses. I defined the variable with the ARGV, 
correct. I'm globbing PDFs just to see if there are any PDFs. If there 
aren't any, then, I exit the script. This script only works on PDFs, in 
that folder. I run this script, along with many, many other scripts like 
it, with a scheduling utility called ActiveBatch. I've told that 
scheduling utility to fire this script when there's one PDF, at least, 
in the folder. So, yes, the ARGV[0] stands for the one file at a time 
way of executing against one or more PDF files. Thanks.
Posted by Alex Gutteridge (Guest)
on 2012-11-05 14:38
(Received via mailing list)
On 05.11.2012 13:01, Peter Bailey wrote:
>>
> that folder. I run this script, along with many, many other scripts
> like
> it, with a scheduling utility called ActiveBatch. I've told that
> scheduling utility to fire this script when there's one PDF, at
> least,
> in the folder. So, yes, the ARGV[0] stands for the one file at a time
> way of executing against one or more PDF files. Thanks.

As Brian says it looks like ARGV[0] is not getting set (i.e.
ActiveBatch is running it without an argument). It's a bit confusing
because you say this will only be run if there are one or more pdf
files, but then explicitly test for whether there are any files - is it
just belt and braces? Is this (untested obvs.) snippet equivalent to
what you want?

require 'rubygems'
require 'fileutils'
require 'net/ftp'

Dir.chdir("F:/workflows/graphics/asura-post/vijay/in")

pdffiles = Dir.glob("*.pdf")
exit if pdffiles.length == 0

FileUtils.cp(pdffiles[0], "//mako/archive/vijay")
FileUtils.mv(pdffiles[0], "../scratch")
Posted by Peter Bailey (peterbailey)
on 2012-11-05 14:41
Alex Gutteridge wrote in post #1082939:
> On 05.11.2012 13:01, Peter Bailey wrote:
>>>
>> that folder. I run this script, along with many, many other scripts
>> like
>> it, with a scheduling utility called ActiveBatch. I've told that
>> scheduling utility to fire this script when there's one PDF, at
>> least,
>> in the folder. So, yes, the ARGV[0] stands for the one file at a time
>> way of executing against one or more PDF files. Thanks.
>
> As Brian says it looks like ARGV[0] is not getting set (i.e.
> ActiveBatch is running it without an argument). It's a bit confusing
> because you say this will only be run if there are one or more pdf
> files, but then explicitly test for whether there are any files - is it
> just belt and braces? Is this (untested obvs.) snippet equivalent to
> what you want?
>
> require 'rubygems'
> require 'fileutils'
> require 'net/ftp'
>
> Dir.chdir("F:/workflows/graphics/asura-post/vijay/in")
>
> pdffiles = Dir.glob("*.pdf")
> exit if pdffiles.length == 0
>
> FileUtils.cp(pdffiles[0], "//mako/archive/vijay")
> FileUtils.mv(pdffiles[0], "../scratch")

OK. Well, I've discovered that this script actually runs just fine in 
the command shell. But, it doesn't run inside my Ruby editor. I get the 
"nil" error when I run the script inside my Ruby editor.  But, if I go 
to the command shell with a PDF file in that directory and run the 
script, it runs fine. So, I've sent an e-mail to my Ruby editor folks. 
Thanks.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.