Create convenience functions (batch commands) in Ruby console - ruby

During development I found myself checking the some results in the Ruby console everyday. This is done by typing the same commands with different parameters every time. However these command can be long, or sometimes several commands need to run sequentially.
For example:
Nokogiri.HTML(open(Rails.root.join('page/p1.html')))
I am wondering if it is possible to specify some convenience functions, which automatically gets loaded when I run the console? So I can call complex calls anytime I want in the console.
I also heard that there are console replacements. So it doesn't have to be the native console, as long as it offers similar functionalities.

Not sure about automatically, but you can create a ruby module, put all your useful stuff in there as methods, and just require/include that at the start of using the console.

As d11wtq pointed out, according to the console you use, you can use the its configuration file to write your own convenience functions:
IRB has ~/.irbrc
PRY has ~/.pryrc
RIB has ~/.rib/config.rb
Place the code in these and you can call them in the console.

Related

Where is the per-method documentation for ruby

I am looking for the primary documentation for ruby functions. As in, the full documentation for the system command.
For example, I have seen one guy who is using the system command with redirection arguments at Running a command from Ruby displaying and capturing the output but someone else who doesn't know that this exists Getting output of system() calls in Ruby
I don't care about the answer to that specific question, but when I search https://www.ruby-lang.org/en/documentation/ which seems authoritative, all I can find are links to third party "guides" or conceptual learning resources. What I need is the ruby equivalent of PHP's function reference at https://php.net/manual/en/funcref.php Or perldoc -f for perl.
http://ruby-doc.org
Packages are organized into "core" and "std-lib".
For example, here is the documentation for the Kernel#system method.

Preventing debugging from stopping at core library modules?

When I start debugging using:
ruby -rdebug file.rb
and place break points:
b path:linenumber
before it gets to my break point, it stops at many other points in the imported libraries.
Does any one know how to prevent that?
Note: I already know I can place debugger points in the code which works well, but I do not want to do that since I keep forgetting to remove them.
I'm more thinking of a solution like using an option to prevent stopping at any module under /usr/lib/ruby.
I haven't used rdebug at all yet, but I've been having a great experience using Pry (http://pryrepl.org/). You simply stick a binding.pry anywhere in your code and upon execution you'll get dropped into the context of your binding.pry. You can then inspect variables, methods, navigate to different contexts using ls and cd, etc.
Sorry if this isn't what you were looking for!

How to debug the code to be tested

When I run an rspec test, I want to get details of the variables in the code that is tested. I tried to use rubymine, but it is complicated to configure. Sublime text 2 is a ruby friendly editor, so I wish someone can recommend some plugins to me.
Unfortunately, I don't know of any Sublime plugins that will do this for you, but...
Check out the pry gem. This will allow you to peek into your code, view instance variables, modify settings, etc. within the middle of an Rspec test.
There is no similar package for Sublime. What you are looking for is a debugger (in fact, RubuyMine integrates the Ruby debugger).
The most common Ruby debugger is the debugger gem. Simply install it, then place a debugger statement where you want to debug the execution and you'll be able to jump in the middle of the running spec execution.

Ruby libraries for command-prompt-based applications

I'm looking to create a Ruby application with a command-prompt-based interface. By "command-prompt-interface," I mean something similar to irb, but note that I do not mean actually executing Ruby code. The gems I'm finding via Google seem to be more suited to CLIs like git, not an actual prompt like irb.
I need to be able to define commands and handlers for those commands, so something like:
(prompt) helloworld
Hello World
(prompt)
Validation and a built-in help system would also be good extras, as would the ability to execute a single command from the command line (calling it from the shell cliapp.rb -c helloworld).
I'm able to create this from scratch, but if there are any libraries available I'd prefer to use that rather than reinventing the wheel.
You may want to try Methadone, which was created by David Bryant Copeland, the author of the book "Build Awesome Command-Line Apps in Ruby". You may want to check out the book as well.
See this blog post for more information.

How can I reload a script in IRB?

I am writing a Ruby script for use in the Rails environment, but I chose to run it from irb because reloading the Rails console can be a pain. Now the wait time is much shorter from irb, but I'm bothered that I have to restart irb and require the script everytime I make a change. Is there a simpler way of reloading a script from irb?
I found a method in this thread, but that only applies to gem files apparently. My require statement looks like this
require "#{File.expand_path(__FILE__)}/../lib/query"
EDIT: Having tried load rather than require, I still couldn't get it to work. I can't get a stop on these errors.
ruby-1.9.2-p0 > load "#{File.expand_path(__FILE__)}/../lib/query.rb"
LoadError: no such file to load -- /Users/newuser/Dropbox/Sites/rails/hacknyc/(irb)/../lib/query.rb
In irb, File.expand_path(__FILE__)} will just return "#{path you ran irb from}/(irb)". Which creates a path that doesn't actually exist. Luckily all file paths are relative to where you ran irb anyway. This means all you need is:
load "lib/query.rb"
If you want to use the __FILE__ in an actual file, that's fine, but don't expect it to produce a valid path in irb. Because an irb there is no "file" at all, so it cannot return valid path at all.
Also, __FILE__ will work fine if used in a file loaded into irb via load or require. Cause that's kinda what it's for.
Instead of using require, try load. The former only loads a source file once, while the latter loads it every time you call it.
according to this link you need to load your file and do not forget the extention.
Here is a fancier version to use too at this link number 2 which could be helpful for you too.
You may want to try hashing out why your rails console isn't working for you though.
I think load is what you are looking for.

Resources