EdvardM
10/5/2015 - 10:56 AM

script to install pry and some other nice tools for Ruby

script to install pry and some other nice tools for Ruby

#!/usr/bin/env bash
#
# Simple script to setup friendly environment for beginning Linux/Mac Ruby users
#

echo "installing pry and awesome_print"
gem install -q --no-ri --no-rdoc \
    pry pry-doc pry-coolline awesome_print

# Nifty pry config copied from https://github.com/dotphiles/dotphiles/blob/master/ruby/aprc

PRYRC="$HOME/.pryrc"

if [ ! -f $PRYRC ]; then
    echo "Writing a nice $PRYRC"
    cat <<EOF > $PRYRC
# === CUSTOM PROMPT ===
# This prompt shows the ruby version (useful for RVM)
prompt_proc = lambda do |obj, nest_level, _|
  ruby_info = ""
  ruby_info << "#{Rails.version}@" if defined?(Rails)
  ruby_info << RUBY_VERSION
  ruby_info = "\e[32m#{ruby_info}\e[0m"
  nest_info = "#{nest_level}"
  obj_info  = "\e[33m#{obj}\e[0m"
  "[#{ruby_info}] #{nest_info}:(#{obj_info}) > "
end

Pry.prompt = [prompt_proc, prompt_proc]

# === Listing config ===
# Better colors - by default the headings for methods are too
# similar to method name colors leading to a "soup"
# These colors are optimized for use with Solarized scheme
# for your terminal
Pry.config.ls.separator = "\n" # new lines between methods
Pry.config.ls.heading_color = :magenta
Pry.config.ls.public_method_color = :green
Pry.config.ls.protected_method_color = :yellow
Pry.config.ls.private_method_color = :bright_black

# === CONVENIENCE METHODS ===
# Stolen from https://gist.github.com/807492
# Use Array.toy or Hash.toy to get an array or hash to play with
class Array
  def self.toy(n=7, &block)
    block_given? ? Array.new(n,&block) : Array.new(n) {|i| i+1}
  end
end

class Hash
  def self.toy(n=7)
    Hash[(Array.toy(n) { |c| (96+(c+1)).chr.to_sym }).zip(Array.toy(n))]
  end
end

# == PLUGINS ===
# awesome_print gem: great syntax colorized printing
# look at ~/.aprc for more settings for awesome_print
begin
  require 'awesome_print'
  # The following line enables awesome_print for all pry output,
  # and it also enables paging
  # Pry.config.print = proc {|output, value| Pry::Helpers::BaseHelpers.stagger_output("=> #{value.ai}", output)}

  # If you want awesome_print without automatic pagination, use the line below
  # Pry.config.print = proc { |output, value| output.puts value.ai }
rescue LoadError => err
  puts "gem install awesome_print  # <-- highly recommended"
end
EOF
else
    echo "$PRYRC found, refusing to overwrite"
fi