Skip to content

Instantly share code, notes, and snippets.

@at1as
Created June 11, 2015 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save at1as/dc3a29d3b68f45d42857 to your computer and use it in GitHub Desktop.
Save at1as/dc3a29d3b68f45d42857 to your computer and use it in GitHub Desktop.
Find Necessary Ruby Method for Desired Output
#!/usr/bin/env ruby
## Find necessary method for desired result
## entering:
## jason$ method_find.rb "HELLO" "hello"
## returns:
## HELLO.downcase == hello
## HELLO.swapcase == hello
def method?(current, desired, debug: false)
valid_methods = []
current.methods.each do |method|
begin
if current.send(method) == desired.to_s
puts "METHOD #{method} produces desired result" if debug
valid_methods << method.to_s
else
puts "METHOD #{method} DOES NOT produce desired result" if debug
end
rescue => e
puts "METHOD #{method} caused an exception : #{e}" if debug
end
end
valid_methods
end
methods = method?(ARGV[0], ARGV[1], debug: false)
puts ?\n
methods.each do |method|
puts "#{ARGV[0]}.#{method} == #{ARGV[1]}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment