DonMB
DonMB

Reputation: 2718

Cannot open file with Ruby Errno::ENOENT: No such file or directory @ rb_sysopen

Using Ruby 2.6.6 I am not able to open a file the regular way.

2.6.6 :006 > File.open "https://www.pocket-rocket.io/robots.txt"
Traceback (most recent call last):
        7: from /Users/martinbraun/.rvm/rubies/ruby-2.6.6/bin/irb:23:in `<main>'
        6: from /Users/martinbraun/.rvm/rubies/ruby-2.6.6/bin/irb:23:in `load'
        5: from /Users/martinbraun/.rvm/rubies/ruby-2.6.6/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
        4: from (irb):6
        3: from (irb):6:in `rescue in irb_binding'
        2: from (irb):6:in `open'
        1: from (irb):6:in `initialize'
Errno::ENOENT (No such file or directory @ rb_sysopen - https://www.pocket-rocket.io/robots.txt)

The file exists and I am able to browse to it using my browser. But I keep getting this error.

I tried:

require 'open-uri'

no success.

What am I doing wrong here? Is it a possible problem with this Ruby version? I could not find any hints.

Upvotes: 0

Views: 1146

Answers (1)

razvans
razvans

Reputation: 3251

File.open expects files to be on the disk, it doesn't work with remote files.

Instead you could do use Net::HTTP.get, which will return a string.

 url = URI('https://www.pocket-rocket.io/robots.txt')
 Net::HTTP.get(url) # => "User-agent: *\nDisallow: /wp-admin/\nAllow: /wp-admin/admin-ajax.php\n"

Upvotes: 1

Related Questions