How can I clear the rails console history - ruby-on-rails

When I run rails c and press the up key when irb starts up, I can see the last commands I entered when my app dropped to irb after encountering a debugger command for the ruby-debug gem. I would not only like to clear these commands out, but I would like it if rails c would pull the last commands I issued during my last rails console session. I think it used to do this but I'm not sure what has changed. I'm on ruby 1.8.7 and rails 3.0.3 on Mac OS 10.6.5 if that helps.
Update
Ray's answer helped me out in the interim. Recently I did a bit more digging to find out more and realized that there are a number of conflicting issues.
IRB checks if you have a ~/.irbrc and if not falls back to /etc/irbrc as Ray mentioned. However, if you are using rvm there is another file to consider ~/.rvm/scripts/irbrc which just loads up ~/.rvm/scripts/irbrc.rb (note the .rb) if you have rvm_path set in your ENV (you should if using rvm).
Interestingly while ~/.rvm/scripts/irbrc.rb was based off of /etc/irbrc they are not the same and differ in a few ways. The most obvious way and easiest way to detect which one is being used on your system is their history file's name. If /etc/irbrc is being used your history file will be ~/.irb_history where as rvm's is ~/.irb-history (Note: _ vs -).
Hopefully this additional information will help you determine what you need to setup your system as you would like.
Pry Concerns
I've since stopped using debugger and have moved to pry-byebug which includes the pry gem. Pry is an alternative to IRB but can also be used along side and within it. The reason I was able to provide the above update is because I was trying to figure out how to keep their respective histories separate. For more information please see my answer to the SO question on "why does pry history keep cloberring irb history?". I've included links there to the known Github issue for Pry as well as my attempt to fix it.

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.

There are a number of tools to help improve the irb experience. Bond and hirb are promising.
Here is Comprehensive list of Irb Tools and some tips on directly editing the .irbrc file.
Hope this help!

Although a very old question I got here by google.
Turns out RVM slightly changed over time.
Currently my IRB history (using rvm) is stored here:
user#host:~$ ls ~/.rvm/rubies/ruby-2.4.2/.irbrc*
/home/user/.rvm/rubies/ruby-2.4.2/.irbrc
/home/user/.rvm/rubies/ruby-2.4.2/.irbrc_history

Related

How to use Nokogiri to scrape data on a cloud development environment (Cloud9)

I'm currently using Cloud9 as a cloud-based IDE, but unfortunately it seems like running things on Cloud9 adds a layer of complexity that I don't quite understand.
For example, the following code, despite being copied and pasted from a tutorial, will not generate any output in the terminal:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
url = "http://stats.nba.com/player/#!/203096/stats/"
data = Nokogiri::HTML(open(url))
puts data
This leads to two questions:
Do I need to deploy this code via something like Heroku, before it can access URLs on the internet? Or will it only work locally until I do that?
Where, exactly, is the output supposed to be when I'm not openly running a Ruby console? I'm simply running the file in bash.
Yes, you can do it:
Goto C9.io and login
Click "Workspace" and choose "Custom Template"
mkdir scrape && cd $_
touch scrape.rb
Paste your code into scrape.rb
Run: ruby scrape.rb
There you go, everything should be fine.
Because you are using OpenUri to get the webpage, this will work on Heroku or on any machine. But you don't need to use Heroku, as you are already able to scrape nba.com on Cloud9.
You said the code you are using doesn't print to console, but it does for me.
It depends what you want to do with the data you parse using Nokogiri. Do you want to save it to a database or file? Look into writing files instead of using the puts command to print to the screen.
This code outputs to the console.
First make sure you installed the Nokogiri gem, and you have full Internet connectivity.
Second start debugging. To debug a program you can use a gem like byebug that gives you an interactive debugger at runtime. Otherwise, as your code is short, you can add a bunch of puts "point x" to actually see where your program halts or break.
Third pay attention to any error or warning you see on the screen.

Paths get messed up whenever I start a new terminal window (OSX)

I'm very new to terminal, and UNIX command line stuff. I had Rails and Ruby set up nicely with the latest version, and created a new Rails project using the rails new command.
Then I decided to look like a hacker, and use the homegrown skin. After loading up a new window, my ruby -v generated 1.8.7, instead of 1.9.3. Rails is no longer a command.
Here is the PATH for the terminal window that has what I want:
PATH=/Users/felix/.rvm/gems/ruby-1.9.3-p0/bin:/Users/felix/.rvm/gems/ruby-1.9.3-p0#global/bin:/Users/felix/.rvm/rubies/ruby-1.9.3-p0/bin:/Users/felix/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:.
I tried looking at those files to see which ones has the path I want. But that lead down this dark and convoluted path that I probably never want to go down again. Are PATH vars supposed to be this long?
This is the PATH for the new terminal window:
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/Users/felix/.rvm/bin
There are also many other differences when I run env, but I won't go into those details (unless they are required) because I feel like fixing the PATH var would fix the other missing vars as well.
So what can I do to remedy this problem? How do I make these variables stick. Also, is my PATH var comically long? Or is this standard.
Nawww, my PATH environment variable is longer than yours. It sounds like you just need to get accustomed to setting things up in the shell you are using.
Find any article on Google that talks about bash and MacOS, and you'll probably find a bit that talks about .bashrc as well.
Like this article.
As for making a permanent change to Ruby, I found this article which says you can use "rvm --default use 1.9.3" and that setting should persist.

Are there any iPython-like shells for Ruby or Rails?

I love iPython and am learning RoR along with some libraries like Mechanize and I'd like to be able to easily see what I'm working with in terms of introspection. I would like to be able to type "." + TAB and see.
There is an irb tool to help autocomplete
require 'irb/completion'
You should take a look at Pry (an IRB alternative and runtime developer console). It isn't as advanced as the current version of IPython, but it's the most advanced developer console we have in Ruby.
try IRB http://en.wikipedia.org/wiki/Interactive_Ruby_Shell, or the rails console http://guides.rubyonrails.org/command_line.html. I don't know if either of them have autocomplete, but they are quality tools.
There is an IPython notebook backed by Ruby kernel. Details about the project
Wirble is a convenient, if infrequently-updated way of bringing together some of these tools. It's not quite as complete as iPython, but gets pretty close. Among other things, it pulls in irb/completion.
For rails, you can do the same, but use script/console from your rails' root directory to start irb.

Auto initialize rails console

I'm always making the same steps when I do:
script/console
do you know some file to edit or something similiar to avoid doing always the same, like for example loading some particular required gem (in my casi 'spawners' :))
Create a file called .irbrc in your home directory and any Ruby code you put in there will be executed at the beginning of your console session. There's lots of cool improvements you can make to the console experience this way, e.g. these from Dr. Nic (the end of that article also has instructions for getting .irbrc working in Windows).
With irb, ~/.irbrc serves for that purpose. Not sure whether Rails console uses it though.

rails ruby-prof and benchmark testing

I'm running Rails 2.2.2. I've read a few articles about ruby-prof and profiling a rails app. And I'm confused as to how things are really working.
I was originally using this tutorial
http://snippets.aktagon.com/snippets/255-How-to-profile-your-Rails-and-Ruby-applications-with-ruby-prof
to profile my app, and it works. This involves writing your own profile environment and running > RAILS_ENV=profiling ./script/performance/request -n 100 profiling/homepage.rb
So here's my confusion. Somehow, this runs ruby-prof and opens up all the stats etc, but I can't see anywhere where ruby-prof is ever actually called.
So then I read further, and it seems 2.2.2 has benchmarking/profiling built in. So I write a test file in the performance section like so
require 'test_helper'
require 'performance_test_help'
# Profiling results for each test method are written to tmp/performance.
class BrowsingTest < ActionController::PerformanceTest
def test_worksheet
get '/reduction/worksheet'
end
end
and run
rake test:profile
Is this equivalent to what I was doing above, but just now it's integrated into the whole rails framework?
My next question is this. The original script ouput a flat file and html file, but I couldn't figure out how to also get a tree file automatically to open up with KCacheGrind, or in my case MacCallGrind. Can I add formats into my script call?
Edit: running the scripts through rake test seem to actually produce a tree file, great. Mac CallGrind however seems to hang when trying to parse it. Anyone know other tools for viewing these tree files?
Turns out, it's exactly the same, I must have been reading an old tutorial. All the benchmarking and profiling is now in Rails which is awesome. And it outputs txt,html and tree files, also awesome. Now if Only MacCallGrind would work, there's no way I'm installing the 4gb of KDE packages needed just to view a tree file.
Brad, you should contact the author of MacCallGrind and see if there's an update, or whether he/she has a fix for your files.
Oh that's right, that's me. Please email at richard at symbol aggmedia.net, as there is an update about to come out, and I'd love to have Ruby/Rails support available.

Resources