[ Content | Sidebar ]

Archives for 2008

Meta-Ruby for the win!

May 17th, 2008

So… let’s suppose you’re hacking away in Ruby and you mistype a method name:

irb(main):001:0> 5.clas
NoMethodError: undefined method `clas' for 5:Fixnum
        from (irb):1

Obviously I *meant* to type ‘class’, but I got the arity (0) right and the spelling wrong.

I propose an extension to Ruby based on the hypothesis that typos are more common than arity errors: if I call an undefined method with arity A on an object then I probably meant to call *one* of the object’s methods of arity A. Just printing an error message is pretty doof since its certainly not what you wanted to happen. A statistically much better solution is to pick one method of the correct arity at random and execute that — it’s going to be right *some* of the time!

How would we do this in Ruby? After little bit of experimentation I produced this:

class Object
  def method_missing(name, *args)
    choices = self.class.instance_methods.collect do |name|
      self.method name
    end
    right_arity = choices.select do |method|
      method.arity == args.length
    end
    which = right_arity[rand(right_arity.length)]
    which.call(*args)
  end
end

Just include this in your program and *all* Ruby objects will have this amazing feature!!!!

Sample run:

irb(main):017:0> 4.asdsa
nil
irb(main):018:0> 4.asdsa
4
irb(main):019:0> 4.asdsa
Fixnum
irb(main):020:0> 4.asdsa
-5
irb(main):023:0> 4.asdsa(7)
11

😀 😀 😀

Cakesniffing on Rails

May 17th, 2008

A Web 2.0 application for cakesniffers everywhere: cakesniffer-or-not.

Telepathic Cows

April 17th, 2008

Next version of xcowsay may feature telepathy

Use case? IRC messages appear as cowz!!! Awesome.

Another xcowsay release!!

April 17th, 2008

Yes yes yes!!! xcowsay 0.7 has been released!!!!

This version has:

  • A config file! checkout the man page for details
  • Can specify as many words as you like on the command line and they’ll all be joined together

Get it here!

xcowsay 0.5

March 7th, 2008

The next release of xcowsay is out!

New features in this version:

  • Click the cow to make it go away
  • Specify the cow size with –cow-size=(small|med|large)

Get it here

xcowsay on OS X

March 4th, 2008

Apparently xcowsay works on a Mac too… woo! 😀

xcowsay 0.4.1

February 29th, 2008

Fixes a bug in xcow(fortune|at) on Ubuntu

xcowsay-0.4.1.tar.gz

xcowsay-0.4

February 28th, 2008

OMG LiKE… xcowsay-0.4 is released. New featurez:

  • Automatically calculates your reading speedz!

Gets it here