# 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.
- module Golfr
- def method_missing( method, *args)
- endings = Regexp.new( /[!?=]?$/ )
- ending = method.to_s =~ endings ? $& : ""
- ( look_for = method.to_s )[ endings ] = ""
- if found_method = ( methods.sort.select{ |m| m =~ /^#{look_for}/ }.first ||
- methods.sort.select{ |m| m =~ /#{look_for}$/ }.last )
- found_method[ endings ] = ""
- STDERR.puts "Calling #{found_method}#{ending} for #{method}" if $DEBUG
- send ( found_method + ending ).to_sym, *args
- else
- raise NoMethodError, "#{method}"
- end
- end
- end
- class Object
- include Golfr
- end
Then have fun!
- require 'golfr'
- a = []
- $><"Hello\n" # Matches '<<'
- a.uns "Cheese" # Matches 'unshift'
- a.ft [ "Pickle" ] # Matches 'unshift'
- puts a.insp # Matches 'inspect'
- a.fl! # Matches 'flatten!'
- puts a.pect # Matches 'inspect'
- puts a.n? # Matches 'nil?'
- puts "carl".c # Matches 'capitalize'
- 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.