Amazing Print with Pry

Public
Great Ruby debugging companion: pretty print Ruby objects to visualize their structure. Supports custom object formatting via plugins.
Icons/chart bar
Used 43 times
Created by
V Viktor Schmidt

Usage

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/X8BsKo"
Template Source

Review the code before running this template on your machine.

def do_bundle
  Bundler.with_original_env { run "bundle install" }
end

def print_green(heredoc)
  puts set_color heredoc, :green
end

def do_commit
  git :init
  git add: "."
  git commit: " -m 'Add Amazing Print with Pry to your rails console' "
end

def has_amazing_print_in_irbrc?
  @has_amazing_print_in_irbrc ||= begin
    checker_regex = /require.*awesome_print.*/
    File.read(".irbrc")[checker_regex]
  end
end

def has_amazing_print_in_pryrc?
  @has_amazing_print_in_pryrc ||= begin
    checker_regex = /require.*awesome_print.*/
    File.read(".pryrc")[checker_regex]
  end
end

say "\nApplying Amazing Print to your rails console..."
# Add Pry as your rails console
rails_command "app:template LOCATION='https://railsbytes.com/script/Xo5sbm'"

inject_into_file 'Gemfile', after: 'group :development, :test do' do
  <<-RUBY 

  # Pretty print Ruby objects to visualize their structure
  gem "amazing_print", "~> 1.4"
  RUBY
end

do_bundle

create_file ".irbrc", "" unless File.exist? ".irbrc"
unless has_amazing_print_in_irbrc?
  prepend_file ".irbrc" do 
    <<~EOF 
      # frozen_string_literal: true

      require "amazing_print"
      AmazingPrint.irb!
    EOF

  end
end

create_file ".pryrc", "" unless File.exist? ".pryrc"
unless has_amazing_print_in_pryrc?
  prepend_file ".pryrc" do 
    <<~EOF 
      # frozen_string_literal: true

      require "amazing_print"
      AmazingPrint.pry!

    EOF
  end
end

say "\nAdding documentation for developers..."
create_file "doc/miscellaneous.md", "# Miscellaneous\n" unless File.exist? "doc/miscellaneous.md"
append_file "doc/miscellaneous.md" do
  <<~EOF  

  ## amazing_print for IRB console

  [amazing_print](https://rubygems.org/gems/amazing_print) pretty print Ruby objects
  to visualize their structure.
  Supports custom object formatting via plugins.

  Add to your `~/.irbrc`:

  ```ruby
  # frozen_string_literal: true

  require "amazing_print"
  AmazingPrint.irb!
  ```

  ## amazing_print for Rails console with Pry

  Add to your `~/.pryrc`:

  ```ruby
  # frozen_string_literal: true

  require "amazing_print"
  AmazingPrint.pry!

  AmazingPrint.defaults = {
    indent: -2,
    limit: true
  }
  ```
  EOF
end

do_commit

print_green "\nAdded Amazing Print successfully!"
Comments
Viktor Schmidt
Changed to amazing_print.