I've been using Pry with Rails for a while via the Pry-Rails Gem.
I want to add Hirb and Awesome Print to Pry, so I've added initialisation code to my ~/.pryrc file as described here and here:
# ~/.pryrc
require 'rubygems'
# Hirb for Tables
begin
require 'hirb'
Hirb.enable
old_print = Pry.config.print
Pry.config.print = proc do |output, value|
Hirb::View.view_or_page_output(value) || old_print.call(output, value)
end
rescue LoadError => err
puts "no hirb :("
end
# Awesome Print
begin
require 'awesome_print'
Pry.config.print = proc { |output, value| output.puts value.ai }
rescue LoadError => err
puts "no awesome_print :("
end
However, when I run $rails c Pry can't find either Hirb or Awesome print.
Why is this?
Related
I'd like the option to use awesome_print in every IRB console or Rails console.
The IRB console is pretty much working satisfactorily right now. If I run irb, I can type require 'awesome_print' and it works.
The Rails console isn't as easy. require 'awesome_print' doesn't work. I apparently have to do this:
> $LOAD_PATH << '~/.rvm/gems/ruby-2.1.8/gems/awesome_print-1.7.0/lib'
After that, require 'awesome_print' works fine.
But I definitely don't want to have to type $LOAD_PATH << '~/.rvm/gems/ruby-2.1.8/gems/awesome_print-1.7.0/lib' and then require 'awesome_print' every single time I open a Rails console just to be able to use awesome_print. That seems ridiculous.
So, how can I permanently add a path to Ruby's $LOAD_PATH?
Note: I don't want to add awesome_print to the Gemfile of any particular project. I want awesome_print to be available to all my Ruby/Rails projects.
You could simply use a a ~/.irbrc file and do:
require 'awesome_print'
Now, open up another IRB prompt:
irb(main):003:0> ap hash
{
"a" => "b"
}
Edit: this isn't working in rails, seems to be a known issue.
puts the following to the .irbrc:
to_load = %w[
awesome_print
coderay
hirb
pry
pry-doc
pry-remote
pry-theme
slop
yard
].join('|')
regexp = Regexp.new( "(#{to_load})" )
Gem.path.each do |path|
Dir.new("#{path}/gems").each do |gem_path|
next if %w[ . .. ].any?{ |d| gem_path == d }
new_el = "#{path}/gems/#{gem_path}/lib"
$LOAD_PATH << new_el if new_el =~ regexp
end
end
I followed the tutorial on hirb rdoc but unfortunately, my rails console is not working at all.
I've already done sudo gem install hirb
and added hirb to my Gemfile:
gem 'hirb', '~>0.7.0'
Then I launched bundle install
And I get this result :
rails c
Loading development environment (Rails 3.2.11)
> require 'hirb'
=> false
> Hirb.enable
=> true
> Municipality.all
Municipality Load (0.8ms) SELECT "municipalities".* FROM "municipalities" ORDER BY name asc
=> [#<Municipality id: 1, district_id: 6, name: "Ambalamanasy II", created_at: "2013-01-16 12:11:45", updated_at: "2013-01-16 12:11:45">,
...
# doesn't work
Could anyone help?
If your using pry as your rails console... add this in your .pryrc file
require 'hirb'
Hirb.enable
old_print = Pry.config.print
Pry.config.print = proc do |output, value|
Hirb::View.view_or_page_output(value) || old_print.call(output, value)
end
Yoshdog's answer is outdated - it returns an error:
output error: # NoMethodError: undefined method `pager' for nil:NilClass
You can fix this by using the updated code from the docs:
begin
require 'hirb'
rescue LoadError
# Missing goodies, bummer
end
if defined? Hirb
# Slightly dirty hack to fully support in-session Hirb.disable/enable toggling
Hirb::View.instance_eval do
def enable_output_method
#output_method = true
#old_print = Pry.config.print
Pry.config.print = proc do |*args|
Hirb::View.view_or_page_output(args[1]) || #old_print.call(*args)
end
end
def disable_output_method
Pry.config.print = #old_print
#output_method = nil
end
end
Hirb.enable
end
This will also allow you to enable/disable Hirb, which may come in handy.
If you using pry it's works for me
$ pwd
/Users/me/path/rails-app
$ ls -la
-rw-r--r-- 1 ryosuke staff 554 12 26 17:50 .pryrc
and
begin
require 'hirb'
rescue LoadError
# Missing goodies, bummer
end
if defined? Hirb
# Slightly dirty hack to fully support in-session Hirb.disable/enable toggling
Hirb::View.instance_eval do
def enable_output_method
#output_method = true
#old_print = Pry.config.print
Pry.config.print = proc do |*args|
Hirb::View.view_or_page_output(args[1]) || #old_print.call(*args)
end
end
def disable_output_method
Pry.config.print = #old_print
#output_method = nil
end
end
Hirb.enable
end
I'm using awesome_print gem to display output from pry to look pretty. Since, awesome_print uses line breaks, long outputs like ap html.chapters.order("position ASC") only shows the end part of the output, How Do I paginate the result to look like its, piped to less or more
Thanks.
Add this to the .pryrc:
begin
require 'awesome_print'
Pry.config.print = proc { |output, value| Pry::Helpers::BaseHelpers.stagger_output("=> #{value.ai}", output) }
rescue LoadError => err
puts "no awesome_print :("
end
https://github.com/pry/pry/wiki/FAQ#wiki-awesome_print
And in case you are unfamiliar with .pryrc:
When pry starts, it checks for a .pryrc file in your home
directory(~/.pryrc) and also for a per-project .pryrc in the current
directory(./.pryrc). Both files are used if they exist, with the file
from your home directory being loaded first.
https://github.com/pry/pry/wiki/Pry-rc
I've got existing rspecs and cucumber features all running fine.
I'm installing spork (spork-rails in fact) to give me some re-run speed up.
I've got rspec running fine with spork.
I've just modified the env.rb as per instructions (very similar to the mods to spec_helper.rb), but I get uninitialized constant Cucumber::Rails when I try to run bundle exec cucubmer --drb.
Rails 3.2 by the way
Any ideas?
Here's my env.rb:
require 'rubygems'
require 'spork'
#uncomment the following line to use spork with the debugger
require 'spork/ext/ruby-debug'
if Spork.using_spork?
Spork.prefork do
require 'rails'
require 'cucumber/rails'
Capybara.default_selector = :css
begin
DatabaseCleaner.strategy = :transaction
rescue NameError
raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end
end
Spork.each_run do
# This code will be run each time you run your specs.
require 'cucumber/rails'
Cucumber::Rails::Database.javascript_strategy = :truncation
ActionController::Base.allow_rescue = false
module NavigationHelpers
def path_to(page_name)
case page_name
when /the home page/
root_path
# Add more page name => path mappings here
else
if path = match_rails_path_for(page_name)
path
else
raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
"Now, go and add a mapping in features/support/paths.rb"
end
end
end
def match_rails_path_for(page_name)
if page_name.match(/the (.*) page/)
return send "#{$1.gsub(" ", "_")}_path" rescue nil
end
end
end
World(NavigationHelpers)
end
else
#omitted
end
For future reference noting down what I did to fix this. In the end I think it was an odd symptom of having referenced cucumber-rails slightly wrongly in the Gemfile.
I was getting errors as well saying:
WARNING: Cucumber-rails required outside of env.rb.
The rest of loading is being defered until env.rb is called.
To avoid this warning, move 'gem cucumber-rails' under only
group :test in your Gemfile
Following the instructions in https://github.com/cucumber/cucumber/issues/249, I fixed this by adding require: false to my Gemfile as follows:
group :test do
gem 'cucumber-rails', require:false
#....
end
How to teach ActiveSupport to not override standard "json" gem behavior?
require "rubygems"
gem "json"
require "json"
class Time
def to_json(options = nil)
"custom string"
end
end
hash = { :x => Time.now }
puts hash.to_json # => {"x":custom string}
gem "activesupport"
require "active_support/core_ext/object" # Somewhere into Rails internals
puts Time.now.to_json # => custom string
puts hash.to_json # => {"x":"2011-02-14T16:30:10+05:00"}
Expected: after require "active_support/core_ext/object" I wanna get {"x":custom string} result.
Rails since v2.3.3 switched to #as_json due to some significant reasons. So dance with it.
http://weblog.rubyonrails.org/2009/7/20/rails-2-3-3-touching-faster-json-bug-fixes
You have to define
class Time
def to_json(options = nil)
"custom string"
end
end
after
gem "activesupport"
require "active_support/core_ext/object"
code.
How about formatting your Time.now value with strftime like Time.now.strftime("format") for the formatting string please see the Ruby Docs.
Or if you don't really want to format it, just use it as a string call Time.now.to_s