whats_up 1.1.1

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.
data/.autotest ADDED
File without changes
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ .DS_Store
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rupee.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ whats\_up
2
+ =========
3
+
4
+ This is a fork of Dr. Nic's excellent little `what_methods` utility, updated a bit to expand
5
+ functionality and bring syntax a bit more in line with the Ruby status quo. It's actually been
6
+ batted around the internet for a while, with rough etchings dating to 2002. So this is my shot at
7
+ it.
8
+
9
+ **Dr. Nic says** (or is heard from echoes traversing the sands of time)**:**
10
+
11
+ This is from Dr. Nic. See http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/
12
+
13
+ Ever asked: "if I have an object, what method can I call on it to get that result?"
14
+
15
+ See if this suits your console cravings:
16
+
17
+ > 3.45.what_equals 3
18
+ 3.45.to_i() == 3
19
+ 3.45.to_int() == 3
20
+ 3.45.floor() == 3
21
+ 3.45.round() == 3
22
+ 3.45.truncate() == 3
23
+ => {:to_i=>3, :to_int=>3, :floor=>3, :round=>3, :truncate=>3}
24
+
25
+ > 3.45.what_equals 4
26
+ 3.45.ceil() == 4
27
+ => {:ceil=>4}
28
+
29
+ > 3.55.what_equals 4
30
+ 3.55.ceil() == 4
31
+ 3.55.round() == 4
32
+ => {:ceil=>4, :round=>4}
33
+
34
+ > 3.45.what_equals /\n/
35
+ 3.45.psych_to_yaml() == "--- 3.45\n...\n"
36
+ 3.45.to_yaml() == "--- 3.45\n...\n"
37
+ 3.45.pretty_inspect() == "3.45\n"
38
+ => {:psych_to_yaml=>"--- 3.45\n...\n", :to_yaml=>"--- 3.45\n...\n", :pretty_inspect=>"3.45\n"}
39
+
40
+ > 3.what_equals 4, 1
41
+ 3 + 1 == 4
42
+ => {:+=>4}
43
+
44
+ Just what you need in the console.
45
+
46
+ Notice the last example: you can pass parameters after the desired result. whats_up will tell you
47
+ what method will return the desired result if you pass those parameters to it.
48
+
49
+ **Bryan says:**
50
+
51
+ This modest little update retains the original `what?` method, but that was from the halcyon days of
52
+ 2006, before the Ruby community had a rough consensus that `?` methods should be returning a true or
53
+ false value (or at least something useable as such). I've aliased that method as `what_equals`.
54
+
55
+ Note also the addition of helpers like `whats_exactly`, which will only find exact matches, and
56
+ `what_matches`, which will match a regular expression:
57
+
58
+ > 5.whats_exactly 5.0
59
+ 5.to_f() == 5.0
60
+ => {:to_f=>5.0}
61
+
62
+ > "hello".what_matches /^\d$/
63
+ "hello".length() == 5
64
+ "hello".size() == 5
65
+ "hello".bytesize() == 5
66
+ "hello".to_i() == 0
67
+ "hello".hex() == 0
68
+ "hello".oct() == 0
69
+ => {:length=>5, :size=>5, :bytesize=>5, :to_i=>0, :hex=>0, :oct=>0}
70
+
71
+ And if you just want to know everything, I've added `what_works_with` that lists the results of all
72
+ current methods and `whats_not_blank_with` that ignores any false, falsy or empty values:
73
+
74
+ > "hello".what_works_with 2
75
+ "hello" <=> 2 == nil
76
+ "hello" == 2 == false
77
+ "hello" === 2 == false
78
+ "hello".eql?(2) == false
79
+ "hello" * 2 == "hellohello"
80
+ "hello" % 2 == "hello"
81
+ "hello"[2] == "l"
82
+ "hello" =~ 2 == nil
83
+ "hello".upto(2) == #<Enumerator: "hello":upto(2)>
84
+ # ...
85
+
86
+ > "hello".whats_not_blank_with 2
87
+ "hello" * 2 == "hellohello"
88
+ "hello" % 2 == "hello"
89
+ "hello"[2] == "l"
90
+ "hello".upto(2) == #<Enumerator: "hello":upto(2)>
91
+ # ...
92
+
93
+ In line with the original `what_methods` gem, you can `require "whats_up/classic"` to enable aliases
94
+ like `what?`, `matches?`, `exactly?`, `works?` and `not_blank?`.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,15 @@
1
+ require "whats_up"
2
+
3
+ class Object
4
+ alias :what? :what_equals
5
+ alias :exactly? :whats_exactly
6
+ alias :matches? :what_matches
7
+ alias :works? :what_works_with
8
+ alias :not_blank? :whats_not_blank_with
9
+ end
10
+
11
+ module WhatsUp
12
+ class MethodFinder
13
+ @@blacklist += %w(what? exactly? matches? works? not_blank?)
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ module WhatsUp
2
+ # The current version
3
+ VERSION = "1.1.1"
4
+ end
data/lib/whats_up.rb ADDED
@@ -0,0 +1,195 @@
1
+ # Some credits:
2
+ # Code this version is based on: Andrew Birkett
3
+ # http://www.nobugs.org/developer/ruby/method_finder.html
4
+ # Improvements from Why's blog entry
5
+ # * what? == - Why
6
+ # * @@blacklist - llasram
7
+ # * clone alias - Daniel Schierbeck
8
+ # * $stdout redirect - Why
9
+ # http://redhanded.hobix.com/inspect/stickItInYourIrbrcMethodfinder.html
10
+ # Improvements from Nikolas Coukouma
11
+ # * Varargs and block support
12
+ # * Improved catching
13
+ # * Redirecting $stdout and $stderr (independently of Why)
14
+ # http://atrustheotaku.livejournal.com/339449.html
15
+ #
16
+ # A version posted in 2002 by Steven Grady:
17
+ # http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/32844
18
+ # David Tran's versions:
19
+ # * Simple
20
+ # http://www.doublegifts.com/pub/ruby/methodfinder.rb.html
21
+ # * Checks permutations of arguments
22
+ # http://www.doublegifts.com/pub/ruby/methodfinder2.rb.html
23
+ #
24
+ # Last updated: 2006/05/20
25
+
26
+ class Object
27
+ def what_equals(*a)
28
+ WhatsUp::MethodFinder.show(self, {}, *a)
29
+ end
30
+
31
+ def whats_exactly(*a)
32
+ WhatsUp::MethodFinder.show(self, { force_exact: true }, *a)
33
+ end
34
+
35
+ def what_matches(*a)
36
+ WhatsUp::MethodFinder.show(self, { force_regex: true }, *a)
37
+ end
38
+
39
+ def what_works_with(*a)
40
+ WhatsUp::MethodFinder.show(self, { show_all: true }, *a)
41
+ end
42
+
43
+ def whats_not_blank_with(*a)
44
+ WhatsUp::MethodFinder.show(self, { show_all: true, exclude_blank: true }, *a)
45
+ end
46
+
47
+ # Make sure cloning doesn't cause anything to fail via type errors
48
+ alias_method :__clone__, :clone
49
+ def clone
50
+ __clone__
51
+ rescue TypeError
52
+ self
53
+ end
54
+ end
55
+
56
+ # A class to suppress anything that would normally output to $stdout
57
+ class DummyOut
58
+ # Does nothing (instead of writing to $stdout)
59
+ def write(*args); end
60
+ end
61
+
62
+ module WhatsUp
63
+ class MethodFinder
64
+ @@blacklist = %w(daemonize display exec exit! fork sleep system syscall what_equals
65
+ whats_exactly what_matches whats_up ed emacs mate nano vi vim)
66
+ @@infixes = %w(+ - * / % ** == != =~ !~ !=~ > < >= <= <=> === & | ^ << >>).map(&:to_sym)
67
+ @@prefixes = %w(+@ -@ ~ !).map(&:to_sym)
68
+
69
+ def initialize(obj, *args)
70
+ @obj = obj
71
+ @args = args
72
+ end
73
+ def ==(val)
74
+ MethodFinder.show(@obj, val, *@args)
75
+ end
76
+
77
+ class << self
78
+ def build_check_lambda(expected_result, opts = {})
79
+ if opts[:force_regex]
80
+ -> a, b { a === b.to_s }
81
+ elsif expected_result.is_a?(Regexp) && !opts[:force_exact]
82
+ -> a, b { a === b.to_s }
83
+ elsif opts[:force_exact]
84
+ -> a, b { a.eql?(b) }
85
+ elsif opts[:show_all]
86
+ if opts[:exclude_blank]
87
+ -> a, b { !b.nil? && !b.empty? }
88
+ else
89
+ -> a, b { true }
90
+ end
91
+ else
92
+ -> a, b { a == b }
93
+ end
94
+ end
95
+
96
+ # Find all methods on [an_object] which, when called with [args] return [expected_result]
97
+ def find(an_object, expected_result, opts = {}, *args, &block)
98
+ check_result = build_check_lambda(expected_result, opts)
99
+
100
+ if opts[:force_regex] && expected_result.is_a?(String)
101
+ expected_result = Regexp.new(expected_result)
102
+ end
103
+
104
+ # Prevent any writing to the terminal
105
+ stdout, stderr = $stdout, $stderr
106
+ $stdout = $stderr = DummyOut.new
107
+
108
+ methods = an_object.methods
109
+
110
+ # Use only methods with valid arity that aren't blacklisted
111
+ methods.select! { |n| an_object.method(n).arity <= args.size && !@@blacklist.include?(n) }
112
+
113
+ # Collect all methods equaling the expected result
114
+ results = methods.inject({}) do |res, name|
115
+ begin
116
+ stdout.print ""
117
+ value = an_object.clone.method(name).call(*args, &block)
118
+ res[name] = value if check_result.call(expected_result, value)
119
+ rescue
120
+ end
121
+ res
122
+ end
123
+
124
+ # Restore printing to the terminal
125
+ $stdout, $stderr = stdout, stderr
126
+ results
127
+ end
128
+
129
+ # Pretty-prints the results of the previous method
130
+ def show(an_object, opts = {}, *args, &block)
131
+ opts = {
132
+ force_regex: false,
133
+ force_exact: false,
134
+ show_all: false,
135
+ exclude_blank: false
136
+ }.merge(opts)
137
+
138
+ expected_result = opts[:show_all] ? nil : args.shift
139
+ found = find(an_object, expected_result, opts, *args, &block)
140
+ prettified = prettify_found(an_object, found, *args)
141
+ max_length = prettified.map { |k, v| k.length }.max
142
+
143
+ prettified.each do |key, value|
144
+ puts "#{key.ljust max_length} == #{value}"
145
+ end
146
+
147
+ found
148
+ end
149
+
150
+ private
151
+
152
+ # Pretty prints a method depending on whether it's an operator, has arguments, is array/hash
153
+ # syntax, etc. For example:
154
+ #
155
+ #
156
+ def prettify_found(an_object, found, *args)
157
+ args = args.map { |o| o.inspect }.join(", ")
158
+ pretty_object = truncate_inspect(an_object, to: 40)
159
+
160
+ found.map do |key, value|
161
+ pretty_key = if @@infixes.include?(key)
162
+ "#{pretty_object} #{key} #{args}"
163
+ elsif @@prefixes.include?(key)
164
+ "#{key.to_s.sub /\@$/, ""}#{pretty_object}"
165
+ elsif key == :[]
166
+ "#{pretty_object}[#{args}]"
167
+ elsif args != ""
168
+ "#{pretty_object}.#{key}(#{args})"
169
+ else
170
+ "#{pretty_object}.#{key}"
171
+ end
172
+
173
+ pretty_value = truncate_inspect(value, to: 120)
174
+
175
+ [pretty_key, pretty_value]
176
+ end
177
+ end
178
+
179
+ # Inspects an object and returns a string representation, truncating it to length in the
180
+ # provided <tt>to:</tt> argument if necessary
181
+ def truncate_inspect(object, opts = {})
182
+ max_length = opts[:to] || 80
183
+ full = object.inspect
184
+
185
+ if full.length > max_length
186
+ left_cutoff = (max_length - 5) * 2 / 3.0
187
+ right_cutoff = max_length - 6 - left_cutoff
188
+ "#{full[0..left_cutoff]} ... #{full[-right_cutoff..-1]}"
189
+ else
190
+ full
191
+ end
192
+ end
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,3 @@
1
+ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
2
+ require "whats_up"
3
+ include WhatsUp
data/whats_up.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "whats_up/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "whats_up"
7
+ s.version = WhatsUp::VERSION
8
+ s.authors = ["Bryan McKelvey", "Dr Nic Williams"]
9
+ s.email = ["bryan.mckelvey@gmail.com", "drnicwilliams@gmail.com"]
10
+ s.homepage = "http://brymck.herokuapp.com/"
11
+ s.summary = %q{Determine what methods can be called on an object that return a given value}
12
+ s.description = %q{Determine what methods can be called on an object that return a given value}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_development_dependency "autotest"
20
+ s.add_development_dependency "bundler"
21
+ s.add_development_dependency "rspec"
22
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: whats_up
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bryan McKelvey
9
+ - Dr Nic Williams
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-03-23 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: autotest
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: bundler
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ description: Determine what methods can be called on an object that return a given
64
+ value
65
+ email:
66
+ - bryan.mckelvey@gmail.com
67
+ - drnicwilliams@gmail.com
68
+ executables: []
69
+ extensions: []
70
+ extra_rdoc_files: []
71
+ files:
72
+ - .autotest
73
+ - .gitignore
74
+ - .rspec
75
+ - Gemfile
76
+ - README.md
77
+ - Rakefile
78
+ - lib/whats_up.rb
79
+ - lib/whats_up/classic.rb
80
+ - lib/whats_up/version.rb
81
+ - spec/spec_helper.rb
82
+ - whats_up.gemspec
83
+ homepage: http://brymck.herokuapp.com/
84
+ licenses: []
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.19
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Determine what methods can be called on an object that return a given value
107
+ test_files:
108
+ - spec/spec_helper.rb