awesomeprint in irb unrecognized - ruby

If I require ap, irb returns true (I assume telling me that the awesomeprint gem has been successfully loaded). However if I issue the command ap f where f is a hash, I get:
NoMethodError: undefined method `ap' for main:Object
from (irb):5
from /usr/local/bin/irb:12:in `<main>'
Thoughts?
Here is my $LOAD_PATH:
"/usr/local/lib/ruby/gems/1.9.1/gems/multi_json-1.1.0/lib", "/usr/local/lib/ruby/gems/1.9.1/gems/multi_xml-0.4.1/lib", "/usr/local/lib/ruby/gems/1.9.1/gems/httparty-0.8.1/lib", "/usr/local/lib/ruby/gems/1.9.1/gems/ap-0.1.1/lib", "/usr/local/lib/ruby/gems/1.9.1/gems/psych-1.2.2/lib", "/usr/local/lib/ruby/gems/1.9.1/gems/crack-0.3.1/lib", "/usr/local/lib/ruby/site_ruby/1.9.1", "/usr/local/lib/ruby/site_ruby/1.9.1/x86_64-darwin11.3.0", "/usr/local/lib/ruby/site_ruby", "/usr/local/lib/ruby/vendor_ruby/1.9.1", "/usr/local/lib/ruby/vendor_ruby/1.9.1/x86_64-darwin11.3.0", "/usr/local/lib/ruby/vendor_ruby", "/usr/local/lib/ruby/1.9.1", "/usr/local/lib/ruby/1.9.1/x86_64-darwin11.3.0"
It seems ap is the 4th one.

The gem you want is called awesome_print, so sudo gem install awesome_print should fix it. May want to remove the other gem 'ap' which seems to be some http and xml related gem.

Related

Method name is in list returned by Object#singleton_methods but not accessible using Object#singleton_method

I'm confused with the difference between Object#singleton_method and Object#singleton_methods.
I thought that the result in Object#singleton_methods is the true set of !!Object#singleton_method(:name), but it seems to be different.
Here is the example script:
require "active_support/deprecation"
# [:debug=, :debug]
ActiveSupport::Deprecation.singleton_methods(false).grep(/debug/)
# [:debug=, :debug]
ActiveSupport::Deprecation.singleton_methods.grep(/debug/)
begin
ActiveSupport::Deprecation.singleton_method(:debug) # exception
rescue => e
puts e.backtrace
raise
end
Gemfile is
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# gem "rails"
gem 'activesupport', '5.1.6'
The result is this:
% bundle install
Fetching gem metadata from https://rubygems.org/..............
Resolving dependencies...
Using concurrent-ruby 1.0.5
Using i18n 1.0.0
Using minitest 5.11.3
Using thread_safe 0.3.6
Using tzinfo 1.2.5
Using activesupport 5.1.6
Using bundler 1.16.1
Bundle complete! 1 Gemfile dependency, 7 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
% bundle exec ruby test.rb
test.rb:8:in `singleton_method'
test.rb:8:in `<main>'
Traceback (most recent call last):
1: from test.rb:8:in `<main>'
test.rb:8:in `singleton_method': undefined singleton method `debug' for `ActiveSupport::Deprecation' (NameError)
I expected that ActiveSupport::Deprecation.singleton_method(:debug) would return #<Method: ActiveSupport::Deprecation.debug>, but raise exception.
I don't understand why. Of course, I know the basic usage in singleton_method and singleton_methods:
# everything is OK!
class Sample; end
class << Sample
def test_method; end
end
# These two expressions behave as I expect.
## [:test_method]
Sample.singleton_methods(false).grep(/test_method/)
## #<Method: Sample.test_method>
Sample.singleton_method(:test_method)
Thanks.
Update: It looks like this is a bug in Ruby. Vasiliy Ermolovich has created an issue along with a patch to address it. The fix has already been merged so this will be resolved in an upcoming update.
This isn't a full answer to your question I'm afraid, but after a bit of digging I've been able to make a minimal example that demonstrates the same thing without the dependency on Active Support. I thought this might still be useful to you for your investigations, or might help someone else who can come along with a complete explanation.
It appears that the unexpected behaviour of singleton_method comes from use of Module.prepend which ActiveSupport::Deprecation uses here.
The same error can be seen with this small example:
module Empty; end
class MyClass
singleton_class.prepend(Empty)
def self.my_method
puts "my method called"
end
end
p MyClass.singleton_methods(:false)
m = MyClass.singleton_method(:my_method)
m.call
Running this gives:
❯ ruby example.rb
[:my_method]
Traceback (most recent call last):
1: from example.rb:11:in `<main>'
example.rb:11:in `singleton_method': undefined singleton method `my_method' for
`MyClass' (NameError)
With the call to prepend removed this runs as you would expect:
❯ ruby example.rb
[:my_method]
my method called

Can't use `gem-install` command in pry

I tried to install a gem by using gem-install command in pry, but it failed.
% pry
pry(main) > gem-install pry-doc
NoMethodError: undefined method `split' for nil:NilClass
from /Users/ironsand/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/pry-0.10.1/lib/pry/rubygem.rb:60:in `install'
I could install the gem by using gem command from bash.
Is there something I must configure to use gem-install command in pry?
I hadn't used PRY for a while.
Looking through the source code found the gem-install command code.
Look at your Gem.configuration['gem'] and you will find that it is nil.
It looks like this comes from your ~/.gemrc settings or default values.
In your ~/.gemrc file, if you place the line gem: ''
This solved the issue for me as it is no longer doing the split on nil, but on the assigned value for gem: key in the .gemrc file.

Ruby Gems "NameError: undefined local variable or method 'update' for main:Object" on every command

I'm attempting to set up a Watir environment. I had several issues actually installing the gems necessary, so I uninstalled and reinstalled Ruby 1.9.3 (I'm running Windows 7.) Now, I can't do any installs, updates, etc. from the ruby command line. Here is an example of some simple commands that should work but are not:
C:\Users\Matt Adams>irb
irb(main):001:0> gem -v
NameError: undefined local variable or method `v' for main:Object
from (irb):1
from C:/Ruby193/bin/irb:12:in `<main>'
irb(main):002:0> gem update
NameError: undefined local variable or method `update' for main:Object
from (irb):2
from C:/Ruby193/bin/irb:12:in `<main>'
I can start ruby irb, but that's it. Its almost as if none of the ruby commands were installed. Anyone have any suggestions? Note that I've already done a re-install.
IRB is there for you to try out Ruby commands or snippets and see immediate responses. If you want to install or update gems, I suggest you get off IRB first by running "quit" and follow whatever instructions you have on your hand.

gem-install of mongoid throws an uninitialized constant in Ruby, works in irb

I am writing a script with Ruby/MongoDB that stores Tweets. After I gem-installed mongoid, this first-steps code throws an error:
require 'rubygems'
require 'mongo'
require 'mongoid'
Mongoid.database = Mongo::Connection.new('localhost').db('db')
# snippet from http://rujmah.posterous.com/using-mongoid-without-rails
NB. This is no Rails app, but a Terminal script.
The error I get is:
./mongoid.rb:10: uninitialized constant Mongoid (NameError)
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /Library/Ruby/Site/1.8/rubygems/custom_require.rb:36:in `require'
from mongoid.rb:3
It works in irb and I'm running ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0].
What am I doing wrong?
Edit August 2012
Somehow I got it to work. Alas, nearly a year on, I really can’t remember how. I will try to do better next time.
I hit the same issue while trying to get Bullet gem to work. The solution for me was to simply move gem 'mongoid', github: 'mongoid/mongoid' as the first line of the Gemfile. I find it really odd but that was how I got rid of that error.
I am using Ruby 2.1.0 and Rails 4.0.0

edit with vim in irb

I'm trying to set up editing within irb using vim as described in this vimcast: http://vimcasts.org/episodes/running-vim-within-irb/
I did the gem install interactive_editor and it seemed to install the gem to /home/me/.gems/ruby/1.9.1/gem/interactive_editor, and I also edited my .irbrc file and added:
require 'rubygems'
require 'interactive_editor'
but when I try to run vi from irb I get this:
NameError: undefined local variable or method `vi’ for main:Object
from (irb):1
I am very new to Ruby/gems etc... I don't even know where to begin. Googling this error didn't really help, I only found a few people with the same issue and no answers.
I'm on Ubuntu 10.04, I have Ruby 1.9.1 installed... not sure about the versions for irb or rubygems.
Thanks a lot!
Here's the output from irb and ruby... something's obviously wrong.
aki#drom:~$ ruby -v
ruby 1.9.0 (2008-10-04 revision 19669) [i486-linux]
aki#drom:~$ irb -v
irb 0.9.5(05/04/13)
aki#drom:~$ irb
irb(main):001:0> require 'rubygems'
=> false
irb(main):002:0> require 'interactive_editor'
LoadError: no such file to load -- interactive_editor
from (irb):2:in `require'
from (irb):2
from /usr/bin/irb:12:in `<main>'
I figured it out, my $GEM_HOME variable was not set properly. I had set it to
/home/me/.gems/ruby/1.9.1/gem/
instead of
/home/me/.gems/ruby/1.9.1/
This fixed it.
Thanks for your help!

Resources