4

I always used the 'y' method to get my results cleaned up a bit in my rails console. However it doesn't seem to work anymore.

I have results to be found, and I can view the result, but just not in a clean manner. This is my rails console:

Loading development environment (Rails 3.2.6)
1.9.3-p286 :001 > y Trip.all
  Trip Load (0.1ms)  SELECT "trips".* FROM "trips" 
NoMethodError: undefined method `y' for main:Object
    from (irb):1
    from /home/timen/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
    from /home/timen/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
    from /home/timen/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
1.9.3-p286 :002 >
CaptainCarl
  • 3,411
  • 6
  • 38
  • 71

1 Answers1

6

It's really the first time in my life that I hear of a y method. I've always used p or pp (the latter after including 'pp') or ap (with the awesome_print gem).

It is not part of IRB, nor of pry.

It probably comes from a specific gem you had installed, so check for recent changes in your gemfile, or maybe you switched to bundler or rvm, losing some globally installed gem.

OH MY GOSH THAT'S THE EDIT

y is provided by the yaml library:

require "yaml"

Add this in some initialization script, or in your .irbrc file.

OH MY GOSH IT DOESN'T WORK!

It works in vanilla IRB, but not in rails console....

NOW THAT'S WHY

Rails console 'y' helper returns NameError rather than yaml-formatting output says that the syck YAML library was phased out in ruby 1.9.3 in favor of psych, which does not have the ymethod.

So, use awesome_print or any of its lesser brethren.

Community
  • 1
  • 1
rewritten
  • 16,280
  • 2
  • 47
  • 50
  • I tried installing 'awesome_print' gem and requiring 'awesome_print' in a Ruby file, but y method is still undefined. You got y method by via awesome_print gem? – Brett Sanders Dec 18 '12 at 04:08
  • @BrettSanders `awesome_print` does not provide the `y` method. It provides the `ap` method, which is much more awesome than `y`! `y` is not available at all in ruby 1.9.3, for the reason stated above (change of underlying YAML library) – rewritten Dec 18 '12 at 17:58