Trying to read an mp3 file in ruby

Asked by At

I am looking a read an mp3 file currently located in my public folder.

I have tried the following:

2.5.3 :016 >     audio_file ='/demo.mp3', 'rb')

And it resulted with the following error:

Traceback (most recent call last):
        3: from (irb):16
        2: from (irb):16:in `new'
        1: from (irb):16:in `initialize'
Errno::ENOENT (No such file or directory @ rb_sysopen - /demo.mp3)

I would assume this is pretty straight forward but for some reason it does not work. Does anybody knows how to do that ? Alternatively, would it be possible to read a file that is located in a server via an http://xxxxxxx.xxxxx/xxxx/mp3 for example ? (This is where the file is located initially).

2 Answers

fphilipe On Best Solutions

You've got the wrong path. /demo.mp3 will look for the file at the root of your file system.

If it's in public/, then you need to specify the path using the relative path public/demo.mp3 since the app is running from the project folder (supposedly) which contains the public/ folder.

Yatin N Desai On

You can also use path "#{Rails.root}" + "/public/demo.mp3"