How Do You Clear The IRB Console?
On Mac OS X or Linux you can use Ctrl + L to clear the IRB screen.
Reset IRB Console
Type
exec($0)
in your irb console session.
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
How do I Quit IRB from the command line? (Using terminal on mac)
If you haven't closed a quote, just put a quote in and hit return.
Exiting from the console can be done by typing exit, though in the circumstance your are would need to hit Control - C
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.
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
PDF Generation Hangs Using PDFkit and Wkhtmotopdf
How to Get Order Username and Provisiondate for All Softlayer MAChines Using Ruby
Ancestry Gem in Rails and Mutli Nesting
How to Convert Timestamp with Ruby
Ruby String Prepend '\' Character
Ruby Get Permutations of All Lengths from a String in Order
Ruby - Append Data to Existing JSON
How to Set Up These Crud Controller Actions for Has_Many_Polymorphs and an Error
Invalid Route Name, Already in Use: 'Admin_Root' (Argumenterror) - Failed Activeadmin Install
Emulating Int64 Overflows in Ruby
Ruby Before_Validation Triggers Infinite Loop of Call Back
Boot Up Rails App, Make Request to App from Outside Local Network
Split String into Equal Slices/Chunks
Ruby, Value Bucketing, Beautify Code
Selenium Can't Find Fields with Type Number