methodfinder 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README.markdown +30 -2
  2. data/lib/methodfinder.rb +10 -0
  3. metadata +4 -12
data/README.markdown CHANGED
@@ -8,7 +8,7 @@ that produce the result when called on the receiver with the arguments.
8
8
  Usage
9
9
  ---
10
10
 
11
- >> Methodfinder.find(10,1,3)
11
+ >> MethodFinder.find(10,1,3)
12
12
  => [:%, :<=>, :>>, :[], :modulo, :remainder]
13
13
  >> MethodFinder.find("abc","ABC")
14
14
  => [:swapcase, :swapcase!, :upcase, :upcase!]
@@ -17,10 +17,38 @@ Usage
17
17
  >> MethodFinder.find(['a','b','c'],['A','B','C']) { |x| x.upcase }
18
18
  => [:collect, :collect!, :map, :map!]
19
19
 
20
+ Thanks to a
21
+ [suggestion](https://github.com/citizen428/methodfinder/issues/closed#issue/3)
22
+ by Ryan Bates, this gem now also provides an alternative interface:
23
+
24
+ >> %w[a b c].find_method { |a| a.unknown(1) ; a == %w[a c] }
25
+ => [:delete_at, :slice!]
26
+ >> 10.find_method { |n| n.unknown(3) == 1 }
27
+ => [:%, :<=>, :>>, :[], :gcd, :modulo, :remainder]
28
+
29
+ Inside `find_method`'s block, the receiver is available as block
30
+ argument the special method `unknown` is used as a placeholder for the
31
+ desired method.
32
+
33
+ Warning
34
+ ---
35
+
36
+ Common sense not included!
37
+
38
+ While I never had any problems with this, it's still better to be
39
+ safe than sorry, so use this with caution and maybe not on production
40
+ data.
41
+
42
+ I initially wrote this for the students of the core Ruby course on
43
+ [RubyLearning](http://rubylearning.org), so Rails is not of interest
44
+ to me (not saying it doesn't work there, just that I test in plain
45
+ IRB, not with `script/console`).
46
+
20
47
  Todo
21
48
  ---
22
49
 
23
- * none at the moment
50
+ * a method black list
51
+ * maybe an alternate form of calling this (issue #3)
24
52
 
25
53
 
26
54
  Thanks
data/lib/methodfinder.rb CHANGED
@@ -1,5 +1,15 @@
1
1
  require 'stringio'
2
2
 
3
+ class Object
4
+ def find_method(*args, &block)
5
+ self.methods.sort.map(&:intern).map do |met|
6
+ self.class.class_eval %{ alias :unknown #{met} }
7
+ obj = self.dup rescue self
8
+ [met, (yield obj rescue nil)]
9
+ end.select(&:last).map(&:first)
10
+ end
11
+ end
12
+
3
13
  class MethodFinder
4
14
  ARGS = {
5
15
  :cycle => [1] # prevent cycling forever
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: methodfinder
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 0
8
- - 1
9
- version: 1.0.1
4
+ prerelease:
5
+ version: 1.1.0
10
6
  platform: ruby
11
7
  authors:
12
8
  - Michael Kohl
@@ -14,7 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2011-03-11 00:00:00 +01:00
13
+ date: 2011-03-31 00:00:00 +02:00
18
14
  default_executable:
19
15
  dependencies: []
20
16
 
@@ -44,21 +40,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
44
40
  requirements:
45
41
  - - ">="
46
42
  - !ruby/object:Gem::Version
47
- segments:
48
- - 0
49
43
  version: "0"
50
44
  required_rubygems_version: !ruby/object:Gem::Requirement
51
45
  none: false
52
46
  requirements:
53
47
  - - ">="
54
48
  - !ruby/object:Gem::Version
55
- segments:
56
- - 0
57
49
  version: "0"
58
50
  requirements: []
59
51
 
60
52
  rubyforge_project:
61
- rubygems_version: 1.3.7
53
+ rubygems_version: 1.6.2
62
54
  signing_key:
63
55
  specification_version: 3
64
56
  summary: A Smalltalk-like Method Finder for Ruby