methodfinder 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +29 -1
  3. data/lib/methodfinder.rb +9 -9
  4. metadata +7 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1f711364eaf7e9772fcdba37d1080618579f7e6f
4
- data.tar.gz: b9bdf0bbdd9c32b299a778fbc80747b9b157adc7
3
+ metadata.gz: 6a9a69705744aeafd4eb6e25c3909a44cdd6e650
4
+ data.tar.gz: a5b5f61da2e85b791b95e954fa9355d6f545ffe6
5
5
  SHA512:
6
- metadata.gz: 55acd3f55bb995667eca8053582cfb34f82a2f83c6a5bbd510ba473c5df15c08d0525ed6027b6d3cae433ac1b6b72912ac59e655055415dfa5656249d5a0ed4b
7
- data.tar.gz: 0f445f405f1d393e8ad8e0338b46997faf89635e78fe3ad85c9a2630e2676956f0d9dbbc0d96542c18d1ec090f63184773b25276928f4ceada1016cf934989d3
6
+ metadata.gz: 0a743f6b177ed4adf72fade44e5a84e8b68ae0f813e6410d70333d83fffb6c641881f6b0127dc5e7ac77a4dec979b972e1148a610e46bfcab98af4dabcd0f453
7
+ data.tar.gz: 7d6463f11b889aa7728d4c595567fb804ee19f1f5ee06ad5a00a03b2666c30fa174ce01e8d4b98c8be14113a3bba6c2b9e51969a0584bb5d2b4dbf553123df49
data/README.rdoc CHANGED
@@ -68,6 +68,33 @@ If the second parameter is omitted, all methods of the class or module will be r
68
68
  MethodFinder.find_in_class_or_module(Math)
69
69
  #=> [:acos, :acosh, :asin ... :tanh]
70
70
 
71
+ == Troubleshooting
72
+
73
+ If the +METHOD_FINDER_DEBUG+ environment variable is set, the name of each candidate method is printed to stderr before it is invoked. This can be useful to identify (and blacklist) misbehaving methods.
74
+
75
+ It can be set on the command line e.g.:
76
+
77
+ $ METHOD_FINDER_DEBUG=1 irb
78
+
79
+ Or inside IRB/Pry:
80
+
81
+ >> ENV['METHOD_FINDER_DEBUG'] = '1'
82
+
83
+ Alternatively both <tt>Object#find_method</tt> and <tt>MethodFinder.find</tt> support an optional keyword argument +debug+ to toggle the behavior on a per-call basis (this argument defaults to the value of the environment variable described above):
84
+
85
+ >> 'a'.find_method(debug: true) { |n| n.unknown == 'A' }
86
+ !
87
+ !=
88
+ [output shortened]
89
+ yellowish
90
+ #=> ["String#capitalize", "String#capitalize!", "String#swapcase", "String#swapcase!", "String#upcase", "String#upcase!"]
91
+ >> MethodFinder.find('a', 'A', debug: true)
92
+ !
93
+ !=
94
+ [output shortened]
95
+ yellowish
96
+ #=> ["String#capitalize", "String#capitalize!", "String#swapcase", "String#swapcase!", "String#upcase", "String#upcase!"]
97
+
71
98
  == Warning
72
99
 
73
100
  Common sense not included!
@@ -83,10 +110,11 @@ I initially wrote this for the students of the core Ruby course on {RubyLearning
83
110
  * Jan Lelis for {implementing blacklists}[https://github.com/citizen428/methodfinder/issues/closed#issue/4].
84
111
  * Brian Morearty for pointing out an {incompatibility with Ruby 1.8.7}[https://github.com/citizen428/methodfinder/pull/5] and adding the {blockless version}[https://github.com/citizen428/methodfinder/pull/6] of <tt>Object#find_method</tt>.
85
112
  * Stefan Kanev for {adding Pry support}[https://github.com/citizen428/methodfinder/pull/7].
113
+ * chocolateboy for {compatibility fixes and updates}[https://github.com/citizen428/methodfinder/pull/8] as well as {the intitial debug implementation}[https://github.com/citizen428/methodfinder/pull/9].
86
114
 
87
115
  == License
88
116
 
89
- Copyright (c) 2011 Michael Kohl
117
+ Copyright (c) 2011-2017 Michael Kohl
90
118
 
91
119
  Permission is hereby granted, free of charge, to any person obtaining a copy
92
120
  of this software and associated documentation files (the "Software"), to deal
data/lib/methodfinder.rb CHANGED
@@ -20,9 +20,10 @@ class Object
20
20
  # 10.find_method(1,3)
21
21
  # #=> ["Fixnum#%", "Fixnum#<=>", "Fixnum#>>", "Fixnum#[]", "Integer#gcd", "Fixnum#modulo", "Numeric#remainder"]
22
22
 
23
- def find_method(*args, &block)
23
+ def find_method(*args, debug: ENV['METHOD_FINDER_DEBUG'], &block)
24
24
  if block
25
25
  mets = MethodFinder.methods_to_try(self).select do |met|
26
+ STDERR.puts met if debug
26
27
  self.class.class_eval("alias :unknown #{met}")
27
28
  obj = self.dup rescue self # dup doesn't work for immutable types
28
29
  block.call(obj) rescue nil
@@ -52,7 +53,6 @@ module MethodFinder
52
53
  CLASS_METHOD_BLACKLIST[:Object] << :pry
53
54
  end
54
55
 
55
- class << self
56
56
  # Provided with a receiver, the desired result and possibly some
57
57
  # arguments, <tt>MethodFinder.find</tt> will list all methods that
58
58
  # produce the given result when called on the receiver with the
@@ -67,13 +67,14 @@ module MethodFinder
67
67
  # MethodFinder.find(['a','b','c'], ['A','B','C']) { |x| x.upcase }
68
68
  # #=> ["Array#collect", "Array#collect!", "Enumerable#collect_concat", "Enumerable#flat_map", "Array#map", "Array#map!"]
69
69
 
70
- def find(obj, res, *args, &block)
70
+ def self.find(obj, res, *args, debug: ENV['METHOD_FINDER_DEBUG'], &block)
71
71
  redirect_streams
72
72
 
73
73
  mets = methods_to_try(obj).select do |met|
74
74
  o = obj.dup rescue obj
75
75
  m = o.method(met)
76
76
  if m.arity <= args.size
77
+ STDERR.puts met if debug
77
78
  a = args.empty? && ARGS.has_key?(met) ? ARGS[met] : args
78
79
  m.call(*a, &block) == res rescue nil
79
80
  end
@@ -84,7 +85,7 @@ module MethodFinder
84
85
  end
85
86
 
86
87
  # Returns all currently defined modules and classes.
87
- def find_classes_and_modules
88
+ def self.find_classes_and_modules
88
89
  constants = Object.constants.sort.map { |c| Object.const_get(c) }
89
90
  constants.select { |c| c.class == Class || c.class == Module}
90
91
  end
@@ -105,7 +106,7 @@ module MethodFinder
105
106
  # MethodFinder.find_in_class_or_module(Math)
106
107
  # #=> [:acos, :acosh, :asin ... :tanh]
107
108
 
108
- def find_in_class_or_module(c, pattern=/./)
109
+ def self.find_in_class_or_module(c, pattern=/./)
109
110
  cs = Object.const_get(c.to_s)
110
111
  class_methods = cs.methods(false) rescue []
111
112
  instance_methods = cs.instance_methods(false)
@@ -114,7 +115,7 @@ module MethodFinder
114
115
  end
115
116
 
116
117
  # Returns a list of candidate methods for a given object. Added by Jan Lelis.
117
- def methods_to_try(obj)
118
+ def self.methods_to_try(obj)
118
119
  ret = obj.methods
119
120
  blacklist = obj.is_a?(Module) ? CLASS_METHOD_BLACKLIST : INSTANCE_METHOD_BLACKLIST
120
121
  klass = obj.is_a?(Module) ? obj : obj.class
@@ -125,17 +126,16 @@ module MethodFinder
125
126
  end
126
127
 
127
128
  # :nodoc:
128
- def redirect_streams
129
+ def self.redirect_streams
129
130
  @orig_stdout = $stdout
130
131
  @orig_stderr = $stderr
131
132
  $stdout = StringIO.new
132
133
  $stderr = StringIO.new
133
134
  end
134
135
 
135
- def restore_streams
136
+ def self.restore_streams
136
137
  $stdout = @orig_stdout
137
138
  $stderr = @orig_stderr
138
139
  end
139
- end
140
140
  private_class_method :redirect_streams, :restore_streams
141
141
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: methodfinder
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Kohl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-06 00:00:00.000000000 Z
11
+ date: 2017-02-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A Smalltalk-like Method Finder for Ruby with some extra features
14
14
  email: citizen428@gmail.com
@@ -16,9 +16,9 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
+ - "./LICENSE"
20
+ - "./README.rdoc"
19
21
  - lib/methodfinder.rb
20
- - ./LICENSE
21
- - ./README.rdoc
22
22
  homepage: http://citizen428.github.com/methodfinder/
23
23
  licenses:
24
24
  - MIT
@@ -29,19 +29,18 @@ require_paths:
29
29
  - lib
30
30
  required_ruby_version: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - '>='
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
34
  version: '0'
35
35
  required_rubygems_version: !ruby/object:Gem::Requirement
36
36
  requirements:
37
- - - '>='
37
+ - - ">="
38
38
  - !ruby/object:Gem::Version
39
39
  version: '0'
40
40
  requirements: []
41
41
  rubyforge_project:
42
- rubygems_version: 2.0.3
42
+ rubygems_version: 2.5.2
43
43
  signing_key:
44
44
  specification_version: 4
45
45
  summary: A Smalltalk-like Method Finder for Ruby
46
46
  test_files: []
47
- has_rdoc: