Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i load a file in ruby on rails console?

am trying to load a file where i have all my setting into rails console. i want to do it because when i use the console there is too much repetition . thank you

like image 920
fenec Avatar asked Apr 05 '10 06:04

fenec


People also ask

What does reload do in Rails console?

Reload: This command will allow you to make changes to your code, and continue to use the same console session without having to restart. Simply type in the “reload!” command after making changes and the console will reload the session.

How do I run a script in Rails?

The simplest way is with rails runner because you don't need to modify your script. runner runs Ruby code in the context of Rails non-interactively. If you make the script executable by running chmod +x script. rb , you can add #!/usr/bin/env rails runner to the top of the script, and then simply run it with ./script.

How do I access the Ruby console?

Interactive Ruby If you're using macOS open up Terminal and type irb , then hit enter. If you're using Linux, open up a shell and type irb and hit enter. If you're using Windows, open Interactive Ruby from the Ruby section of your Start Menu.


2 Answers

You can set configs of IRB using .irbrc file located in your home directory.

You can use load, require and whatever you want there.

Adding something like require 'rubygems' and require 'pp' will help. Some people customize the file more heavily. See this for example.

like image 159
TK. Avatar answered Sep 19 '22 18:09

TK.


From within Rails console or IRB you can load a file with the require method. For example require foo.rb will execute all the statements in foo.rb. You can use relative or absolute paths.

like image 39
Gordon Wilson Avatar answered Sep 21 '22 18:09

Gordon Wilson