methodfinder 1.1.1 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +43 -19
- data/lib/methodfinder.rb +36 -22
- metadata +2 -2
data/README.markdown
CHANGED
@@ -1,22 +1,18 @@
|
|
1
1
|
Description
|
2
2
|
---
|
3
3
|
|
4
|
-
This project was inspired by Smalltalk's Method Finder
|
5
|
-
|
6
|
-
Provided with a receiver, a desired result and possibly some
|
7
|
-
arguments, `MethodFinder.find` will list all methods that produce the
|
8
|
-
given result when called on the receiver with the provided arguments.
|
9
|
-
|
10
|
-
This gem also adds `Object#find_method`, which besides offering an
|
11
|
-
alternate interface to pretty much the same functionality, also allows
|
12
|
-
you to test for state other than the return value of the method.
|
13
|
-
|
14
|
-
All of this probably sounds more complicated than it really is, just
|
15
|
-
look at the examples below.
|
4
|
+
This project was originally inspired by Smalltalk's Method Finder, but
|
5
|
+
additonal features were added over time.
|
16
6
|
|
17
7
|
Usage
|
18
8
|
---
|
19
9
|
|
10
|
+
### MethodFinder.find
|
11
|
+
|
12
|
+
Provided with a receiver, the desired result and possibly some
|
13
|
+
arguments, `MethodFinder.find` will list all methods that produce the
|
14
|
+
given result when called on the receiver with the provided arguments.
|
15
|
+
|
20
16
|
>> MethodFinder.find(10,1,3)
|
21
17
|
=> [:%, :<=>, :>>, :[], :modulo, :remainder]
|
22
18
|
>> MethodFinder.find("abc","ABC")
|
@@ -26,9 +22,12 @@ Usage
|
|
26
22
|
>> MethodFinder.find(['a','b','c'],['A','B','C']) { |x| x.upcase }
|
27
23
|
=> [:collect, :collect!, :map, :map!]
|
28
24
|
|
29
|
-
|
30
|
-
|
31
|
-
|
25
|
+
### Object#find_method
|
26
|
+
|
27
|
+
This gem also adds `Object#find_method`, which besides offering an
|
28
|
+
alternate interface to pretty much the same functionality as
|
29
|
+
`MethodFinder.find`, also allows you to test for state other than
|
30
|
+
the return value of the method.
|
32
31
|
|
33
32
|
>> %w[a b c].find_method { |a| a.unknown(1) ; a == %w[a c] }
|
34
33
|
=> [:delete_at, :slice!]
|
@@ -39,6 +38,30 @@ Inside `find_method`'s block, the receiver is available as block
|
|
39
38
|
argument and the special method `unknown` is used as a placeholder for
|
40
39
|
the desired method.
|
41
40
|
|
41
|
+
### MethodFinder.find\_classes\_and_modules
|
42
|
+
|
43
|
+
A simple method to return all currently defined modules and classes.
|
44
|
+
|
45
|
+
>> MethodFinder.find_classes_and_modules
|
46
|
+
=> [ArgumentError, Array, BasicObject, Bignum ... ZeroDivisionError]
|
47
|
+
|
48
|
+
### MethodFinder.find\_in\_class\_or_module
|
49
|
+
|
50
|
+
Searches for a given name within a class. The first parameter can
|
51
|
+
either be a class object, a symbol or a string whereas the optional
|
52
|
+
second parameter can be a string or a regular expression:
|
53
|
+
|
54
|
+
>> MethodFinder.find_in_class_or_module('Array', 'shuff')
|
55
|
+
=> [:shuffle, :shuffle!]
|
56
|
+
>> MethodFinder.find_in_class_or_module(Float, /^to/)
|
57
|
+
=> [:to_f, :to_i, :to_int, :to_r, :to_s]
|
58
|
+
|
59
|
+
If the second parameter is omitted, all methods of the class or module
|
60
|
+
will be returned.
|
61
|
+
|
62
|
+
>> MethodFinder.find_in_class_or_module(Math)
|
63
|
+
=> [:acos, :acosh, :asin ... :tanh]
|
64
|
+
|
42
65
|
Warning
|
43
66
|
---
|
44
67
|
|
@@ -56,14 +79,15 @@ IRB, not with `script/console`).
|
|
56
79
|
Todo
|
57
80
|
---
|
58
81
|
|
59
|
-
* a method black list
|
60
|
-
* maybe an alternate form of calling this (issue #3)
|
61
|
-
|
82
|
+
* a method black list (maybe)
|
62
83
|
|
63
84
|
Thanks
|
64
85
|
---
|
65
86
|
|
66
|
-
* Matthew Lucas for packaging
|
87
|
+
* Matthew Lucas for first packaging this as a gem.
|
88
|
+
* Ryan Bates for
|
89
|
+
[suggesting](https://github.com/citizen428/methodfinder/issues/closed#issue/3)
|
90
|
+
what eventually became `Object#find_method`.
|
67
91
|
|
68
92
|
License
|
69
93
|
---
|
data/lib/methodfinder.rb
CHANGED
@@ -15,33 +15,47 @@ class MethodFinder
|
|
15
15
|
:cycle => [1] # prevent cycling forever
|
16
16
|
}
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
18
|
+
class << self
|
19
|
+
def find(obj, res, *args, &block)
|
20
|
+
redirect_streams
|
21
|
+
|
22
|
+
obj.methods.sort.map(&:intern).select do |met|
|
23
|
+
o = obj.dup rescue obj
|
24
|
+
m = o.method(met)
|
25
|
+
if m.arity <= args.size
|
26
|
+
a = args.empty? && ARGS.has_key?(met) ? ARGS[met] : args
|
27
|
+
m.call(*a, &block) == res rescue nil
|
28
|
+
end
|
27
29
|
end
|
30
|
+
ensure
|
31
|
+
restore_streams
|
28
32
|
end
|
29
|
-
ensure
|
30
|
-
restore_streams
|
31
|
-
end
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
$stderr = StringIO.new
|
38
|
-
end
|
34
|
+
def find_classes_and_modules
|
35
|
+
constants = Object.constants.sort.map { |c| Object.const_get(c) }
|
36
|
+
constants.select { |c| c.class == Class || c.class == Module}
|
37
|
+
end
|
39
38
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
39
|
+
def find_in_class_or_module(c, pattern=/./)
|
40
|
+
cs = Object.const_get(c.to_s)
|
41
|
+
class_methods = cs.methods(false) rescue []
|
42
|
+
instance_methods = cs.instance_methods(false)
|
43
|
+
all_methods = class_methods + instance_methods
|
44
|
+
all_methods.grep(/#{pattern}/).sort
|
45
|
+
end
|
44
46
|
|
47
|
+
def redirect_streams
|
48
|
+
@orig_stdout = $stdout
|
49
|
+
@orig_stderr = $stderr
|
50
|
+
$stdout = StringIO.new
|
51
|
+
$stderr = StringIO.new
|
52
|
+
end
|
53
|
+
|
54
|
+
def restore_streams
|
55
|
+
$stdout = @orig_stdout
|
56
|
+
$stderr = @orig_stderr
|
57
|
+
end
|
58
|
+
end
|
45
59
|
private_class_method :redirect_streams, :restore_streams
|
46
60
|
end
|
47
61
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: methodfinder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.
|
5
|
+
version: 1.2.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Michael Kohl
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-09 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|