HowTo get ruby to report a sumbolic link?

I am creating a series of symbolic links to target directories. I wish
to check to see if the symbolic link exists or not. When I do this
(using rspec):

File.stat(@soft_link_target).ftype.should == ‘link’

I get this:

  expected: "link",
       got: "directory" (using ==)

Which tells me that the link is there but it is reporting the target
directory and not the link itself. How do I tell if the thing that I am
checking is a symbolic link to a directory and not the directory itself?

On Thu, Jul 22, 2010 at 1:08 PM, James B. [email protected]
wrote:

Which tells me that the link is there but it is reporting the target
directory and not the link itself. How do I tell if the thing that I am
checking is a symbolic link to a directory and not the directory itself?

ri File::Stat tells me that there’s a #symlink? method you could use:

File.stat( @soft_link_target ).should be_symlink

… I believe that’s the right syntax for rspec, but you get the idea.

Ben

On Thu, Jul 22, 2010 at 2:26 PM, James B. [email protected]
wrote:

As File::stat automatically follows symbolic links, symlink? will always
be false for an object returned by File::stat.

You’re right. Sorry, jumped the gun. There’s also File.symlink?,
which does work correctly.

Ben

Ben B. wrote:

ri File::Stat tells me that there’s a #symlink? method you could use:

File.stat( @soft_link_target ).should be_symlink

… I believe that’s the right syntax for rspec, but you get the idea.

rdoc also says:

As File::stat automatically follows symbolic links, symlink? will always
be false for an object returned by File::stat.

Ben B. wrote:

You’re right. Sorry, jumped the gun. There’s also File.symlink?,
which does work correctly.

Yes, it does. Which surprises me since that is what I started with. I
suppose something else must have been wrong in the test and I simply
misdiagnosed the reason.

Thanks.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs