Am 26.07.2013 00:42, schrieb Thomas Luedeke:
> Having trouble finding a sound method that doesn't break when waiting
> for a file to appear.  My initial approach was this:
> 
> while File.exists?( "filename" ) == false
>    sleep( 5 )
> end
> 
> This worked for a while, but doesn't seem to be robust.

What do you mean by not robust?

> I then tried this:
> 
> until File.exists?( "filename" )
>    sleep( 5 )
> end
> 
> Which also doesn't seem to work.

`while !condition' and `until condition' are exactly the same
(like `if !condition' and `unless condition').

> I'm running Ruby 1.8.7.

Unless you have a good reason to specifically use 1.8.7,
you should upgrade, it's not maintained by the Ruby core team anymore.

>  Is there a better way to do this?

There already are a couple of gems to watch directories or files,
have a look at e.g. the `fssm' gem or at `Guard', both should work
on all common platforms. But without knowing what exactly you want
to achieve, it's difficult to make a specific recommendation.

Example for fssm:

  require 'fssm'

  # monitor all txt files in the working directory
  FSSM.monitor('.', '*.txt') do
    create {|base, relative| puts "File #{relative} has been created" }
    update {|base, relative| puts "File #{relative} has been updated" }
    delete {|base, relative| puts "File #{relative} has been deleted" }
  end

Of course, instead of just printing a message you could trigger
arbitrary actions.


Regards,
Marcus


-- 
GitHub: https://github.com/stomar/
PGP:    0x6B3A101A