Reset IRB Console
Type
exec($0)
in your irb console session.
How Do You Clear The IRB Console?
On Mac OS X or Linux you can use Ctrl + L to clear the IRB screen.
Reload the rails console
Yes, you need to call reload!
as this will clear the loaded constants that have been loaded and will load them as they're referenced in the console.
If you have old objects from before the reload!
you will need to call reload
on these individual objects or find new objects and work with them if you want to try out the new method.
As an alternative, I would really recommend looking into a testing framework such as RSpec which gives you repeatable tests and a safety net for your application.
It looks like you're trying to use the console as a testing tool for new functionality in your application, which is what RSpec is better suited for. The console is really good for experimentation.
How can I clear the rails console history
I interpret you question as asking how to turn history on in the Rails Console and off in the Ruby debugger. If this isn't true, please clarify.
IRB, and by extension, the Rails Console, read from ~/.irbrc
, or if that doesn't exist, /etc/irbrc
, to startup and configure irb. Your history is typically written to ~/.irb_history
, but that is dictated by the contents of your irbrc
file. The /etc/irbrc
on my Mac OS X is set up to write the history from irb, so perhaps you've created a local .irbrc
that doesn't have history, or perhaps you have a syntax error in that file.
The debugger reads a file called .rdebugrc
on startup. You can turn off history in debug by adding this line to ~/.rdebugrc
:
set history save off
Turn it back on with:
set history save on
You could also set your debug output to go to a different file than irb reads from with the command:
set history filename
These also work from the debug prompt, but aren't persistent.
clear all variables in rails console
local_variables.each { |e| eval("#{e} = nil") }
local_variables
returns the list of symbols of all local variables in the current scope
a, b = 5, 10
local_variables # => [:b, :a]
Using each
you can iterate over this list an use eval
to assign their values to nil
.
You can also do the same thing with instance_variables
and global_variables
. For example
(local_variables + instance_variables).each { |e| eval("#{e} = nil") }
By the way, if you are going to use it more than once, it might be helpful to define such method in ~/.irbrc
file to make it accessible for all irb sessions (didn't test it in rails console).
class Binding
def clear
eval %q{ local_variables.each { |e| eval("#{e} = nil") } }
end
end
Then, inside irb
session
a = 5
binding.clear
a # => nil
PRY or IRB - reload class and forget deleted functionality
You can use remove_const to remove the class from its parent, either from the Module
it is in:
My::Module.send(:remove_const, :MyClass)
or from Object
if it was not declared inside a Module:
Object.send(:remove_const, :MyClass)
How to suppress the output of return value in IRB/Rails Console?
If you just want to suppress long output once in a while, use ;0
, like:
a = [*1..10000];0
# => 0
If you want to suppress it generally, use the ~/.irbrc file. The IRB.conf[:INSPECT_MODE] and IRB.conf[:PROMPT][your_prompt][:RETURN] control what is returned. You can figure out what your_prompt is by checking IRB.conf[:PROMPT_MODE]
Example:
IRB.conf[:PROMPT][:DEFAULT][:RETURN] = "" # suppress return value completely
You'll need to restart irb after changing the value.
Hope that helps.
How to suppress Rails console/irb outputs
You can append ; nil
to your statements.
Example:
users = User.all; nil
irb
prints the return value of the last executed statement; thus in this case it'll print only nil
since nil
is the last executed valid statement.
Related Topics
Routing Error - Uninitialized Constant
Weird Rails Error "Permission Denied: Bin/Rails" for Old Rails Apps
Missing a Template for This Request Format and Variant
<Rubygems> How to Change Gem Environment Settings
Why Is Devise Not Displaying Authentication Errors on Sign in Page
How to Alias a Class Method in Rails Model
Do You Know an Alternative Ctags Generator for Ruby
Ruby on Rails - Differentiating Plural VS Singular Resource in a Rest API
How to Make Users Automatically Follow Admin User on Sign Up
How to Make a Gemset in Rvm the Default
Rails 4.1 Can't Deploy via Capistrano 3
Normalizing Line Endings in Ruby
How to Sort by Multiple Conditions with Different Orders
How to Create Md5 Hash with Hmac Module in Ruby
How to Run My Ruby Code After Rails Server Start
Most of My Assets Suddenly Return 404 After a Push to Heroku