Archive for GuessMethod

MountainWest RubyConf videos (including my lightning talk on GuessMethod)

Confreaks have started putting up more video from MountainWest RubyConf, and today saw the addition of the lightning talks from day one. I’d like to draw particular attention to the second entry of this list of who talked when about what:

  1. Binary Lottery in Shoes - Mike Moore - 0:10
  2. GuessMethod - Chris Shea (me!) - 4:35
  3. ActiveScaffold - Ed Moss - 8:51
  4. Migration Concordance - Josh Susser - 12:39
  5. Framework Components - Jeremy McAnally - 18:05
  6. Systems Building Systems with Puppet - Andrew Schafer - 20:43
  7. Multi-image Uploading with SWFUpload and Rails - David South - 27:14
  8. RubyAmp: Ruby Dev in TextMate Amplified - Tim Harper - 31:55
  9. Build a GUI App in 5 Minutes - David Koontz - 37:46

Lots of interesting stuff in there. And I encourage everyone to look at other videos from the conference. It was great.

Comments

GuessMethod 0.2.1

I gave a lightning talk about GuessMethod at MountainWest RubyConf, and then I looked at the code again. I figured it could use a little cleaning, so I took my steel wool to it.

I fixed up some of the documentation and the website, made the specs run again (they always passed, they just stopped running at some point… rspec change?), and rewrote the code that finds close matches of missing methods and constant names. And that last change (based on a naïve benchmark) doubled its speed (of course, iterating over an array once instead of twice will sort of do that).

So, if you need aggressive spell-checking in your irb or script/console sessions check it out:

001:0> Strin.tos
attention: replacing non-existant constant Strin with String for Object
attention: sending to_s instead of tos to String:Class
“String”

Or, if you sometimes can’t be bothered to correctly remember rake task names (and rake -T takes too long), use grake!

mvb:~/hot-rails-app cms$ grake db:migrat
(in /Users/cms/hot-rails-app)
attention: invoking task db:migrate instead of db:migrat

It might show up in Giles Bowkett’s utility_belt at some point, in case you’re into that and you can’t be bothered with this:

sudo gem install guessmethod

Comments

Rails, .irbrc, fixes

It looks like Giles Bowkett’s Utility Belt gem is having the same sort problems with Rails as GuessMethod, i.e. loading .irbrc anywhere that isn’t irb or script/console breaks things. His problem was one of method name conflicts and visibility, but the ultimate solution is the same: leave .irbrc alone when loading script/server! As Giles says, this should be considered a bug in Rails…

…but it appears to have been fixed in Rails 2.0.x, so any temporary hacks can go away as soon as everyone upgrades.

Comments (2)

irb, Rails, GuessMethod, and some magic new to me

I was looking at the code for Giles Bowkett’s utility_belt gem, and came across something I wish I had known about ages ago. There’s a way to execute some code in an .irbrc after everything else when the irb session starts (technically, when the context changes). The problem with GuessMethod and Rails was that GuessMethod can’t load first. Problem solved.

So now my .irbrc has this line to include GuessMethod in a way that works with script/console:

IRB.conf[:IRB_RC] = Proc.new { require 'guessmethod' }

Of course, if you’re using that IRB.conf[:IRB_RC] for something else (like using utility_belt), something’s going to overwrite something.

Anyway, I’ve finally just given up and wrapped my entire .irbrc in a big unless:

unless $0 == 'script/server'

I’m confused as to why script/server loads .irbrc for every request, but at least this mitigates that issue a bit. (As an aside, can someone tell me why it does that?)

Comments

GuessMethod 0.2.0!

I was talking yesterday to Corey (of mini-magick fame) and he suggested something for GuessMethod. I implemented it and sent him an instant message demonstrating such. He said, “Oh yeah! I totally want this. This will enrage so many people! I love it.”

What is it? GuessMethod for Rake.

mvb:~/hot-rails-app cms$ grake db:migrat
(in /Users/cms/hot-rails-app)
attention: invoking task db:migrate instead of db:migrat

You can use your new grake command, or add require 'guessmethod/rake' to your rakefiles for this functionality. This is probably ten times more dangerous than plain old GuessMethod. Please, oh please, don’t use this in production.

Otherwise, things are pretty much the same:

001:0> Strin.tos
attention: replacing non-existant constant Strin with String for Object
attention: sending to_s instead of tos to String:Class
“String”

Get it:

sudo gem install guessmethod

Comments

GuessMethod 0.1.2

I’m starting to see a pattern. I make one change to GuessMethod, release it, then think of something else almost immediately and release again. And such brings us to 0.1.2.

I almost called this one 0.2 because I think I finally figured out how GuessMethod should really work, which is this way:

  1. Call the original method_missing or const_missing and let it go through unless…
  2. it raises a NameError or NoMethodError, in which case…
  3. capture the error and…
  4. try to guess.

If the guess is successful, apply the guess. If not, raise the original error.

Now this all seems so obvious. But so much is in hindsight.

Anyway, don’t let anyone try to trick you into thinking this is somehow something to be used in production, or even outside of irb. This doesn’t belong anywhere except in irb.

I’ve been trying to think of how to put this in your .irbrc so that things go the best way possible. I think this is it:

require 'guessmethod' if defined?(IRB) and not ENV.has_key?('RAILS_ENV')

Go get it. I swear I wouldn’t work on this unless it made irb more fun, which it totally does.

Comments

GuessMethod 0.1.1 - Now works with Rails!

It finally happened. I figured out how to get GuessMethod (particularly the GuessConstant half of things) and Rails to behave. There’s still a little kludginess to it, but they can work magic together:

mvb:~/rails/depot cms$ script/console
Loading development environment.
>> require 'guessmethod'
=> ["GuessMethod", "GuessConstant", "GuessMethodOptions", "GuessMethodGuesser", "GuessMethodOutputter"]
>> LineItm.find(:first)
no constant in threshold: for LineItm, sending to Object’s const_missing
NameError: uninitialized constant LineItm
        from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:266:in `load_missing_constant’
        from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:452:in `const_missing’
        from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/active_support/dependencies.rb:464:in `unguessed_const_missing’
        from /usr/local/lib/ruby/gems/1.8/gems/guessmethod-0.1.0/lib/guessmethod.rb:87:in `const_missing’
        from (irb):2
>> LineItem
=> LineItem
>> LineItm.find(:first)
attention: replacing non-existant constant LineItm with LineItem for Object
=> #<LineItem:0×30c1338 @attributes={”order_id”=>”1″, “total_price”=>”3″, “quantity”=>”1″, “product_id”=>”8″, “id”=>”1″}>

Once Rails loads a constant GuessConstant can find it, and your poor typing stops holding you back.

There’s just one thing (there’s always just one thing). Things don’t go very well if you require GuessMethod in your irbrc. You have to require it manually in the console. It works fine when script/server gets a hold of it, but not for script/console. I suppose they load everything in a different order (but this isn’t something I’ve looked into yet). Rails has to show up to the dance first.

It was this old Dr. Nic post that got me over the hurdle solving this one. Thanks Dr. Nic.

sudo gem install guessmethod

Enjoy!

Comments (2)

GuessMethod 0.1.0!

I’ve read around the internet that at RailsConfEurope, Dr. Nic used my baby GuessMethod as an example of fun that can be had with meta-programming. I’m flattered.

But flattery only lasts so long, and before I knew it I remembered that I had been wanting to fix it up a little. So I did.

As such, I’m pleased to announce version 0.1 of my little aggressive spelling corrector for irb.

sudo gem install guessmethod

And live the life!

003:0> Strin.tos
attention: replacing non-existant constant Strin with String for Object
attention: sending to_s instead of tos to String:Class
“String”

This is not for production! And it’s not for Rails either. It kills Rails. Until I figure out how to make GuessMethod not kill Rails, it will kill Rails.

That being said, I use it all the time. My irb sessions are full of pretty messages and corrected typos.

Enjoy!

Comments (1)

Writing specs for GuessMethod

I finally got around to writing some specs for GuessMethod. I know the idea is to do that while programming, but I still haven’t quite fully made that part of my habit. This is the sweet spot of the tests so far though:

Cass.nw.clss.should == Clas

Maybe having Clas on the right side of the == is going overboard, but this is what GuessMethod is for: fixing your mistakes so that you don’t have to.

Comments

GuessMethod 0.0.5

GuessMethod, the aggressive spell checker for irb, can now be turned off!

Every once in a while, GuessMethod will get in the way. I’m trying to fix that, but in the meantime, you can just set GuessMethod::Options[:active] = false if you need it to leave everything alone for a little while. Just set it back to true when you want it back. This is handy for people (like me, now) who have require 'guessmethod' in their .irbrc (or config/environments/development.rb for Railsers).

And GuessMethod now includes/extends itself in Object when you require it. That happened in 0.0.4.

It’s been interesting to use GuessMethod regularly (especially considering I deemed it a bad idea). Sometimes I see it trying to handle calls I had no idea were being handled by an object’s method_missing.

Give it a shot if you haven’t already:

sudo gem install guessmethod

Comments

« Previous entries