[Skip To Content]


# Tired of typing long method names?

A bit of Friday fun. Golfr saves you all the hassle of having to type out full method names by letting you use as few characters as you need. Look at the examples to see how it can save you valuable keystrokes.

  1. module Golfr
  2. def method_missing( method, *args)
  3. endings = Regexp.new( /[!?=]?$/ )
  4. ending = method.to_s =~ endings ? $& : ""
  5. ( look_for = method.to_s )[ endings ] = ""
  6. if found_method = ( methods.sort.select{ |m| m =~ /^#{look_for}/ }.first ||
  7. methods.sort.select{ |m| m =~ /#{look_for}$/ }.last )
  8. found_method[ endings ] = ""
  9. STDERR.puts "Calling #{found_method}#{ending} for #{method}" if $DEBUG
  10. send ( found_method + ending ).to_sym, *args
  11. else
  12. raise NoMethodError, "#{method}"
  13. end
  14. end
  15. end
  16. class Object
  17. include Golfr
  18. end

Then have fun!

  1. require 'golfr'
  2. a = []
  3. $><"Hello\n" # Matches '<<'
  4. a.uns "Cheese" # Matches 'unshift'
  5. a.ft [ "Pickle" ] # Matches 'unshift'
  6. puts a.insp # Matches 'inspect'
  7. a.fl! # Matches 'flatten!'
  8. puts a.pect # Matches 'inspect'
  9. puts a.n? # Matches 'nil?'
  10. puts "carl".c # Matches 'capitalize'
  11. Thread.a = true # Matches 'abort_on_exception='

Set $DEBUG=1 if you want to see what methods are actually being called, and feel free to do what you want with the code. It’s all yours.

More about this site...

Last Week’s Top 5 Albums (More »)

  1. LuckyNada Surf
  2. PlansDeath Cab for Cutie
  3. Narrow StairsDeath Cab for Cutie
  4. A Hundred Million SunsSnow Patrol
  5. Old World Underground, Where Are You Now?Metric