Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial import

  • Loading branch information...
commit 4590731ce1a6441f220e2dcdf5207b862f80f3e7 0 parents
@citizen428 citizen428 authored
Showing with 83 additions and 0 deletions.
  1. +48 −0 README.markdown
  2. +35 −0 method_finder.rb
48 README.markdown
@@ -0,0 +1,48 @@
+Description
+---
+
+A Smalltalk-like Method Finder for Ruby. Provided with a receiver, a
+desired result and possibly some arguments, it will list all methods
+that produce the result when called on the receiver with the arguments.
+
+Usage
+---
+
+ >> Methodfinder.find(10,1,3)
+ => [:%, :<=>, :>>, :[], :modulo, :remainder]
+ >> MethodFinder.find("abc","ABC")
+ => [:swapcase, :swapcase!, :upcase, :upcase!]
+ >> MethodFinder.find(10,100,2)
+ => [:**]
+ >> MethodFinder.find(['a','b','c'],['A','B','C']) { |x| x.upcase }
+ => [:collect, :collect!, :map, :map!]
+
+Todo
+---
+
+* Redirect `stdout` and `stderr` to a StringIO object instead of `/dev/null` unless someone tells me that the latter works on Windows.
+* Maybe add some sort of method blacklist so they won’t get tried in the block.
+* Package this as a gem.
+
+License
+---
+
+Copyright (c) 2011 Michael Kohl
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
35 method_finder.rb
@@ -0,0 +1,35 @@
+class MethodFinder
+ ARGS = {
+ :cycle => [1] # prevent cycling forever
+ }
+
+ def self.find(obj, res, *args, &block)
+ redirect_streams
+
+ obj.methods.sort.map(&:intern).select do |met|
+ o = obj.dup rescue obj
+ m = o.method(met)
+ if m.arity <= args.size
+ a = args.empty? && ARGS.has_key?(met) ? ARGS[met] : args
+ m.call(*a, &block) == res rescue nil
+ end
+ end
+ ensure
+ restore_streams
+ end
+
+ def self.redirect_streams
+ @orig_stdout = $stdout
+ @orig_stderr = $stderr
+ $stdout = File.new('/dev/null', 'w')
+ $stderr = File.new('/dev/null', 'w')
+ end
+
+ def self.restore_streams
+ $stdout = @orig_stdout
+ $stderr = @orig_stderr
+ end
+
+ private_class_method :redirect_streams, :restore_streams
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.