Relative paths for run_file


Hi all

I’m still very new to Sonic Pi but I have a lot of experience programming. I am coding a 12 bar blues and I would like to keep a directory in which each file represents a bass line. The main loop will then choose one file from there and play the bass line in it. :slight_smile:

I found the “run_file” function in the documentation but I was wondering if there is a good way to use relative paths with it. Because Sonic Pi doesn’t really execute a Ruby file directly, I can’t use Ruby’s __ FILE __. Besides, I’ve seen Sam Aaron’s comment saying you shouldn’t treat Sonic Pi as Ruby. :slight_smile:

Is there any good way to do this or should I just keep a variable containing the absolute path to my bass lines directory at the top of the buffer?

Thanks in advance


I think you need an absoulte path, but you can mitigate this by using tilde ~ for the root directory. I usually use this for samples putting them in ~/samples and you could have ~/runfile_programs This at least makes it easier to transfer programs between say a raspberry pi and a mac.
The problem with relative files is that sonic-pi is located in different places according to machine. Thus in Raspberry Pi it is in /opt/sonic-pi, in a Mac usually it will be in /Applications (in fact it can be anywhere!). The external files that Sonic Pi uses are placed in .sonic-pi which is located in the user’s home directory (i.e. ~ ).
There is a sonic-pi file init.rb which is placed in this .sonic-pi folder which is run automatically when Sonic Pi is booted. You could set up a path variable to suit your system in that. e.g.

set :rfpath, "~/path/to/my/files/" #nb include trailing /

In a program where you wanted to use run_file you could them use

run_file get(:rfpath)+"my_runfile_program.rb"

and you wouldn’t have to specify the path directly on each occasion.


I know I’m replying pretty late but I just wanted to say thank you for the info. This is pretty much what I expected already.