methodfinder 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/LICENSE +20 -0
  2. data/README.markdown +48 -0
  3. data/lib/methodfinder.rb +35 -0
  4. metadata +66 -0
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Michael Kohl
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
20
+
data/README.markdown ADDED
@@ -0,0 +1,48 @@
1
+ Description
2
+ ---
3
+
4
+ A Smalltalk-like Method Finder for Ruby. Provided with a receiver, a
5
+ desired result and possibly some arguments, it will list all methods
6
+ that produce the result when called on the receiver with the arguments.
7
+
8
+ Usage
9
+ ---
10
+
11
+ >> Methodfinder.find(10,1,3)
12
+ => [:%, :<=>, :>>, :[], :modulo, :remainder]
13
+ >> MethodFinder.find("abc","ABC")
14
+ => [:swapcase, :swapcase!, :upcase, :upcase!]
15
+ >> MethodFinder.find(10,100,2)
16
+ => [:**]
17
+ >> MethodFinder.find(['a','b','c'],['A','B','C']) { |x| x.upcase }
18
+ => [:collect, :collect!, :map, :map!]
19
+
20
+ Todo
21
+ ---
22
+
23
+ * Redirect `stdout` and `stderr` to a StringIO object instead of `/dev/null` unless someone tells me that the latter works on Windows.
24
+ * Maybe add some sort of method blacklist so they won’t get tried in the block.
25
+ * Package this as a gem.
26
+
27
+ License
28
+ ---
29
+
30
+ Copyright (c) 2011 Michael Kohl
31
+
32
+ Permission is hereby granted, free of charge, to any person obtaining a copy
33
+ of this software and associated documentation files (the "Software"), to deal
34
+ in the Software without restriction, including without limitation the rights
35
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
36
+ copies of the Software, and to permit persons to whom the Software is
37
+ furnished to do so, subject to the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be included in
40
+ all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
43
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
44
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
45
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
46
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
47
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
48
+ THE SOFTWARE.
@@ -0,0 +1,35 @@
1
+ class MethodFinder
2
+ ARGS = {
3
+ :cycle => [1] # prevent cycling forever
4
+ }
5
+
6
+ def self.find(obj, res, *args, &block)
7
+ redirect_streams
8
+
9
+ obj.methods.sort.map(&:intern).select do |met|
10
+ o = obj.dup rescue obj
11
+ m = o.method(met)
12
+ if m.arity <= args.size
13
+ a = args.empty? && ARGS.has_key?(met) ? ARGS[met] : args
14
+ m.call(*a, &block) == res rescue nil
15
+ end
16
+ end
17
+ ensure
18
+ restore_streams
19
+ end
20
+
21
+ def self.redirect_streams
22
+ @orig_stdout = $stdout
23
+ @orig_stderr = $stderr
24
+ $stdout = File.new('/dev/null', 'w')
25
+ $stderr = File.new('/dev/null', 'w')
26
+ end
27
+
28
+ def self.restore_streams
29
+ $stdout = @orig_stdout
30
+ $stderr = @orig_stderr
31
+ end
32
+
33
+ private_class_method :redirect_streams, :restore_streams
34
+ end
35
+
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: methodfinder
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Michael Kohl
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-02-11 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: citizen428@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/methodfinder.rb
31
+ - ./LICENSE
32
+ - ./README.markdown
33
+ has_rdoc: true
34
+ homepage: https://github.com/citizen428/MethodFinder
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.7
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: A Smalltalk-like Method Finder for Ruby
65
+ test_files: []
66
+