Read local/remote file from rails console
1 min readJul 7, 2018
One fine day, I was running a database query to load records of specific id’s using Heroku console.
Pasting a large size of id-list into Heroku console that too in shorter time was very challenging.
As a solution, I had stored the id-list into Github gist and loaded it in Heroku console by using open-uri.
open-uri is part of the standard Ruby library, and it will redefine the behavior of open
so that you can open a url, as well as a local file. It returns a File
object, so you should be able to call methods like read
and readlines
require 'open-uri'
file_contents = open('local_path/local_file.txt') { |f| f.read }
web_contents = open('http://www.remote_url.com') { |f| f.read }