Skip to content

Instantly share code, notes, and snippets.

@cybersamx
Last active July 26, 2023 18:40
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cybersamx/5d4f635e1d7a0f8ed264 to your computer and use it in GitHub Desktop.
Save cybersamx/5d4f635e1d7a0f8ed264 to your computer and use it in GitHub Desktop.
Load and execute a Ruby script in Rails console
# Say you need to run this script which is not part of your Rails app...
# Do the following:
#
# 1. Assume we put this script file in <Rails app root>/tmp
# 2. $ cd <Rails app root>
# 3. $ rails console
# 4. ruby > load "#{Rails.root}/tmp/script_to_run_in_rails.rb"
# 5. ruby > MyClass.echo
# Hello
# => nil
#
# Just run load again to reload the changes in script_to_run_in_rails.rb.
class MyClass
def self.echo
puts('Hello')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment