Skip to content

Instantly share code, notes, and snippets.

@ethagnawl
Created March 20, 2013 20:44
Show Gist options
  • Save ethagnawl/5208253 to your computer and use it in GitHub Desktop.
Save ethagnawl/5208253 to your computer and use it in GitHub Desktop.
DSL for querying nth_letter of string.
def self.method_missing(method, *args)
number_dictionary = {
first: 1,
second: 2,
third: 3,
fourth: 4,
fifth: 5,
sixth: 6,
seventh: 7,
eigth: 8,
ninth: 9,
tenth: 10
}
if method.to_s =~ /(.+)_letter/ && number = number_dictionary[$1.to_sym]
args[0][number - 1]
else
super
end
end
first_letter('hey') # h
second_letter('hey') # e
third_letter('hey') # y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment