Skip to content

Instantly share code, notes, and snippets.

@shidel-dev
Last active August 29, 2015 14:07
Show Gist options
  • Save shidel-dev/3b4f7ddc2b23b10c67af to your computer and use it in GitHub Desktop.
Save shidel-dev/3b4f7ddc2b23b10c67af to your computer and use it in GitHub Desktop.
module Magic
def self.respond_to?(method_sym, include_private = false)
if method_sym.to_s =~ /^find_where_(.*)$/
true
else
super
end
end
def self.method_missing(m, *args, &block)
split_call = m.to_s.split('_')
if split_call[0] == "find"
finder(split_call[-1],args[0])
end
end
def self.finder(term, hash)
if term =~ /^[0-9]$/
term = term.to_i
end
hash.find_all{ |pair| pair[1] == term }
end
end
#-----tests----
joe_hash = { student_A:'joe', student_B:'what', student_C:'joe' }
p Magic.find_where_joe(joe_hash)
cat_hash = { 1 => Array.new, 2 => "mouse", 3 => "cat" }
p Magic.find_where_cat(cat_hash)
num_hash = { a: 2, b:'dog' , c: 23, d: 54, e: 23 }
p Magic.find_where_23(num_hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment