console snippets

Solution to "require': no such file to load -- readline (LoadError)" problem

Tagged readline, ruby, console, compile  Languages bash

If you've compiled Ruby from source, you might get this error when executing script/console:

/usr/local/lib/ruby/1.8/irb/completion.rb:10:in require': no such file to load -- readline (LoadError)

One way of fixing this is to compile readline, which is distributed along with the Ruby source:

cd /opt/src/ruby-1.8.5-p2/ext/readline
ruby extconf.rb
make
sudo make install

This works even after compiling Ruby, so no need to recompile... If you're wondering what readline is then this quote from the project homepage sums it up in one sentence: "The GNU Readline library provides a set of functions for use by applications that allow users to edit command lines as they are typed in."

How to run ActiveRecord migrations from the console

Tagged activerecord, console, migrations, schema  Languages ruby

There are multiple ways of running migrations from the console. This might be the easiest one to remember:

irb ActiveRecord::Schema
add_index :followers, [:leader_id, :follower_id], :unique => true

You can also paste the migration code into the console window:

class ModifyRating < ActiveRecord::Migration
  def self.up
    change_column :products, :rating, :decimal, :precision => 5, :scale => 3
  end

  def self.down
  end
end

Run the migration by typing ModifyRating.up.

How to customize Hirb output

Tagged hirb, irb, console, rails  Languages ruby

Only print id, created_at and title for FeedItem class:

Hirb.disable
  Hirb.enable :output => {
    "FeedItem"=>{
      :options=>{
        :fields=>%w{id created_at title}
      }
    }
  }

How to Add a Console to Sinatra Applications

Tagged sinatra, pry, console  Languages ruby

In your application directory, create .pryrc:

Pry.config.editor = "gvim --nofork"

# Print Ruby version at startup
Pry.config.hooks.add_hook(:when_started, :say_hi) do
  puts "Using Ruby version #{RUBY_VERSION}"
end

# Require Sinatra application
require 'config/environment'

Now you can start the console by typing:

$ pry
# or with bundler :(
$ bundle exec pry

Rails Console Update Problems?

Tagged rails, pry, guard, console, tty  Languages bash

Console not updating when typing? Pry/Ruby/Rails/something messing up console output? Check your stty settings:

stty -a

Is echo disabled. Do you see "-echo" in the output?

Try resetting the console:

reset
# or
stty

Check if you have incorrectly colorized output: http://stackoverflow.com/questions/8806643/colorized-output-breaks-linewrapping-with-readline/8916332#8916332

Are you using a gem or something else that colorizes console output? Try disabling them.

Examples: * turn * pry-theme

See: http://askubuntu.com/questions/171449/shell-does-not-show-typed-in-commands-reset-works-but-what-happened

How to show Rails environment in Rails/Pry console

Tagged rails, pry, console  Languages ruby

Show Rails environment in Rails/Pry console by adding this to ~/.pryrc:

def env
  env = Rails.env
  if env.production? 
    "\e[1;31m#{env}\e[0m - " 
  else
    "\e[1;30m#{env}\e[0m - " 
  end
end

prompt = env

Pry.config.prompt = [
  proc { |obj, nest_level, _| "#{prompt}(#{obj}):#{nest_level} > " },
  proc { |obj, nest_level, _| "#{prompt}(#{obj}):#{nest_level} * " }
]

Pry.config.hooks.add_hook(:before_session, :show_ruby_version) do
  puts "Ruby #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL}"
end

Environment name is shown in red, if in production. Note: use pry-rails gem…