Question

Ok so with siriproxy it my lib folder along with the rb file for the plugin I have created a myconfig.yml file so I can change certain settings by writing to that file.

I have been able to write to the file but only if I include the full path all the way from the home directory down.

But is there not a way to open the file from the same directory i am in? I have tried every path combination I can think of.

There has to be one i am missing

Was it helpful?

Solution

If you use the following in your ruby file, you should get the absolute path where it is

File.expand_path(__FILE__)

From doc __FILE__

The name of the file currently being executed, including path relative to the directory where the application was started up (or the current directory, if it has been changed)

From doc File.expand_path

Converts a pathname to an absolute pathname.

As you probably want the directory, you should use File.dirname(__FILE__), so the path of your file myconfig.yml should be obtained with

File.join(File.expand_path(File.dirname(__FILE__)), 'myconfig.yml')

In more recent Ruby (>=2.0.0), you can use __dir__ (from Archonic's comment):

Returns the canonicalized absolute path of the directory of the file from which this method is called. It means symlinks in the path is resolved. If FILE is nil, it returns nil. The return value equals to File.dirname(File.realpath(FILE)).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top