Pysis
Pysis

Reputation: 1606

Rails REPL that is more than irb/pry but less than rails console

I frequently want to try out small code snippets, often much smaller than classes, and even functions, just to make sure it works by itself so I don't need to test it by running a bunch of scripts, simply to fix small errors in a line of code or so.

Besides irb/pry, I want to test Rails-specific code, such as Object.blank?. So with that, I want to have the Rails library loaded, but I don't need the full functionality that Rails console gives me. Especially when the application is not in a working state, the REPL will not open at all, and merely present a stack trace of the failure at hand.

If anybody knew how to achieve this middle ground, maybe through the use of a particular gem path and require statement to load one of the other REPLs I have mentioned, could you illustrate those commands?

I am working inside of a project using RVM to manage the gemset, and would like to not modify that environment at all, maybe only my general terminal environment, if possible.

Upvotes: 0

Views: 1451

Answers (2)

user229044
user229044

Reputation: 239501

Most of the Rails "magic" come from Active Support Core Extensions. You can include that in a regular irb session to get most of the connivence methods like blank?.

Start an IRB session and run

require 'active_support'
require 'active_support/core_ext'

Upvotes: 1

Martin Tournoij
Martin Tournoij

Reputation: 27852

.blank? is from ActiveSupport. You can actually just load ActiveSupport without the rest of Rails:

irb(main):001:0>  require 'active_support/all'
irb(main):002:0> [].blank?
=> true

The all.rb file loads all of ActiveSupport.

The same can be done with ActiveRecord and other rails components; for example:

irb(main):001:0> require 'active_record'
=> true
irb(main):002:0> class NewModel < ActiveRecord::Base; end
=> nil
irb(main):003:0> NewModel.new
ActiveRecord::ConnectionNotEstablished: No connection pool for NewModel

This gives an error because I didn't bother setting up a database, but it shows that Rails is pretty modular. I've used ActiveRecord in projects without Rails (the rails gem is actually an empty gem which just defines the various active_* gems as dependencies).

Upvotes: 2

Related Questions