Skip to content

Instantly share code, notes, and snippets.

@btoone
Forked from petekeen-cf/eval_gist.rb
Last active February 1, 2023 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btoone/099da5fb29149bec881636d8c501c4a3 to your computer and use it in GitHub Desktop.
Save btoone/099da5fb29149bec881636d8c501c4a3 to your computer and use it in GitHub Desktop.
Eval Gist
# Usage:
#
# Rails
# Step 1: write a gist with a file named __script__.rb
# Step 2: copy and paste the function def below into Rails console
# Step 3: run `eval_gist("the-gist-id-from-the-url")`
# Step 4: GOTO Step 3
#
# Ruby
# Start an IRB session and load this file
# $ irb
# > load 'eval_gist.rb'
# > eval_gist("the-gist-id-from-the-url")
#
require 'uri'
require 'net/http'
require 'json'
def eval_gist(gist_id)
uri = URI("https://api.github.com/gists/#{gist_id}")
req = Net::HTTP::Get.new(uri)
Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
resp = http.request(req)
eval(JSON.parse(resp.body).dig("files", "__script__.rb", "content"))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment