├── Guardfile ├── lib ├── interesting_methods │ └── version.rb └── interesting_methods.rb ├── bin ├── setup └── console ├── .gitignore ├── test ├── test_objects │ ├── pig_with_colours_extended.rb │ ├── pig_with_colours_included.rb │ ├── pig.rb │ ├── colours.rb │ └── animal.rb ├── test_helper.rb └── interesting_methods_test.rb ├── Gemfile ├── Rakefile ├── interesting_methods.gemspec ├── LICENSE.txt ├── Gemfile.lock └── README.md /Guardfile: -------------------------------------------------------------------------------- 1 | guard :minitest do 2 | watch(/test|lib/) { 'test' } 3 | end 4 | -------------------------------------------------------------------------------- /lib/interesting_methods/version.rb: -------------------------------------------------------------------------------- 1 | module InterestingMethods 2 | VERSION = '0.1.2' 3 | end 4 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | IFS=$'\n\t' 4 | set -vx 5 | 6 | bundle install 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.bundle/ 2 | /.yardoc 3 | /_yardoc/ 4 | /coverage/ 5 | /doc/ 6 | /pkg/ 7 | /spec/reports/ 8 | /tmp/ 9 | -------------------------------------------------------------------------------- /test/test_objects/pig_with_colours_extended.rb: -------------------------------------------------------------------------------- 1 | class PigWithColoursExtended < Pig 2 | 3 | extend Colours 4 | 5 | end 6 | -------------------------------------------------------------------------------- /test/test_objects/pig_with_colours_included.rb: -------------------------------------------------------------------------------- 1 | class PigWithColoursIncluded < Pig 2 | 3 | include Colours 4 | 5 | end 6 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | git_source(:github) {|repo_name| "https://github.com/#{repo_name}" } 4 | 5 | # Specify your gem's dependencies in interesting_methods.gemspec 6 | gemspec 7 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rake/testtask" 3 | 4 | Rake::TestTask.new(:test) do |t| 5 | t.libs << "test" 6 | t.libs << "lib" 7 | t.test_files = FileList["test/**/*_test.rb"] 8 | end 9 | 10 | task :default => :test 11 | -------------------------------------------------------------------------------- /test/test_objects/pig.rb: -------------------------------------------------------------------------------- 1 | class Pig < Animal 2 | 3 | def self.three_little_pigs 4 | ['Larry', 'Mo', 'Curly'] 5 | end 6 | 7 | def roll_around_in_mud 8 | 3.times do 9 | puts 'Rolling and getting dirty ...' 10 | end 11 | puts 'Oh so dirty!' 12 | end 13 | 14 | end 15 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | require 'minitest/autorun' 2 | require 'interesting_methods' 3 | require 'interesting_methods/version' 4 | require 'test_objects/animal' 5 | require 'test_objects/colours' 6 | require 'test_objects/pig' 7 | require 'test_objects/pig_with_colours_extended' 8 | require 'test_objects/pig_with_colours_included' 9 | -------------------------------------------------------------------------------- /lib/interesting_methods.rb: -------------------------------------------------------------------------------- 1 | class Object 2 | 3 | def im 4 | interesting_methods 5 | end 6 | 7 | def interesting_methods 8 | if self.class == Module 9 | (singleton_methods + instance_methods).uniq.sort 10 | else 11 | (public_methods - Object.methods).sort 12 | end 13 | end 14 | 15 | def self.interesting_methods 16 | (public_methods - Object.methods).sort 17 | end 18 | 19 | end 20 | -------------------------------------------------------------------------------- /bin/console: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'bundler/setup' 4 | require 'interesting_methods' 5 | require 'pry' 6 | 7 | require_relative '../test/test_objects/animal' 8 | require_relative '../test/test_objects/colours' 9 | require_relative '../test/test_objects/pig' 10 | require_relative '../test/test_objects/pig_with_colours_extended' 11 | require_relative '../test/test_objects/pig_with_colours_included' 12 | 13 | Pry.start 14 | -------------------------------------------------------------------------------- /test/test_objects/colours.rb: -------------------------------------------------------------------------------- 1 | module Colours 2 | 3 | def instance_red 4 | '#FF0000' 5 | end 6 | 7 | def instance_green 8 | '#00FF00' 9 | end 10 | 11 | def instance_blue 12 | '#0000FF' 13 | end 14 | 15 | def double_vision 16 | 'hola' 17 | end 18 | 19 | def self.double_vision 20 | 'hola' 21 | end 22 | 23 | def self.class_red 24 | 'rgb(255, 0, 0)' 25 | end 26 | 27 | def self.class_green 28 | 'rgb(0, 255, 0)' 29 | end 30 | 31 | def self.class_blue 32 | 'rgb(0, 0, 255)' 33 | end 34 | 35 | end 36 | -------------------------------------------------------------------------------- /test/test_objects/animal.rb: -------------------------------------------------------------------------------- 1 | class Animal 2 | 3 | @@all = [] 4 | 5 | attr_reader :type, :sound 6 | 7 | def initialize(type, sound) 8 | @type = type 9 | @sound = sound 10 | @@all << self 11 | end 12 | 13 | def speak 14 | puts @sound 15 | end 16 | 17 | def speak_loudly 18 | puts @sound.upcase 19 | end 20 | 21 | def self.all 22 | @@all 23 | end 24 | 25 | def self.report 26 | puts "#{'Type'.rjust(15)} #{'Sound'.rjust(15)}" 27 | puts "#{'----'.rjust(15)} #{'-----'.rjust(15)}" 28 | all.each do |animal| 29 | puts "#{animal.type.rjust(15)} #{animal.sound.rjust(15)}" 30 | end 31 | end 32 | 33 | def b 34 | end 35 | 36 | def a 37 | end 38 | 39 | def c 40 | end 41 | 42 | def self.y 43 | end 44 | 45 | def self.z 46 | end 47 | 48 | def self.x 49 | end 50 | 51 | end 52 | -------------------------------------------------------------------------------- /interesting_methods.gemspec: -------------------------------------------------------------------------------- 1 | lib = File.expand_path('../lib', __FILE__) 2 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 3 | 4 | require 'interesting_methods/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = 'interesting_methods' 8 | spec.version = InterestingMethods::VERSION 9 | spec.authors = ['Sean Lerner'] 10 | spec.email = ['sean@@smallcity.ca'] 11 | spec.summary = 'See relevant methods in your repl by adding .im to any object' 12 | spec.homepage = 'https://github.com/seanlerner/interesting_methods' 13 | spec.license = 'MIT' 14 | spec.files = `git ls-files`.split.reject { |f| f.match('test') } 15 | spec.require_paths = ['lib'] 16 | 17 | spec.add_development_dependency 'bundler', '~> 1.16' 18 | spec.add_development_dependency 'guard', '~> 2.14' 19 | spec.add_development_dependency 'guard-minitest', '~> 2.4' 20 | spec.add_development_dependency 'minitest', '~> 5.0' 21 | spec.add_development_dependency 'pry', '~> 0.11' 22 | spec.add_development_dependency 'rake', '~> 10.0' 23 | end 24 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Sean Lerner 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | interesting_methods (0.1.2) 5 | 6 | GEM 7 | remote: https://rubygems.org/ 8 | specs: 9 | coderay (1.1.2) 10 | ffi (1.9.23) 11 | formatador (0.2.5) 12 | guard (2.14.2) 13 | formatador (>= 0.2.4) 14 | listen (>= 2.7, < 4.0) 15 | lumberjack (>= 1.0.12, < 2.0) 16 | nenv (~> 0.1) 17 | notiffany (~> 0.0) 18 | pry (>= 0.9.12) 19 | shellany (~> 0.0) 20 | thor (>= 0.18.1) 21 | guard-compat (1.2.1) 22 | guard-minitest (2.4.6) 23 | guard-compat (~> 1.2) 24 | minitest (>= 3.0) 25 | listen (3.1.5) 26 | rb-fsevent (~> 0.9, >= 0.9.4) 27 | rb-inotify (~> 0.9, >= 0.9.7) 28 | ruby_dep (~> 1.2) 29 | lumberjack (1.0.12) 30 | method_source (0.9.0) 31 | minitest (5.11.3) 32 | nenv (0.3.0) 33 | notiffany (0.1.1) 34 | nenv (~> 0.1) 35 | shellany (~> 0.0) 36 | pry (0.11.3) 37 | coderay (~> 1.1.0) 38 | method_source (~> 0.9.0) 39 | rake (10.5.0) 40 | rb-fsevent (0.10.3) 41 | rb-inotify (0.9.10) 42 | ffi (>= 0.5.0, < 2) 43 | ruby_dep (1.5.0) 44 | shellany (0.0.1) 45 | thor (0.20.0) 46 | 47 | PLATFORMS 48 | ruby 49 | 50 | DEPENDENCIES 51 | bundler (~> 1.16) 52 | guard (~> 2.14) 53 | guard-minitest (~> 2.4) 54 | interesting_methods! 55 | minitest (~> 5.0) 56 | pry (~> 0.11) 57 | rake (~> 10.0) 58 | 59 | BUNDLED WITH 60 | 1.16.1 61 | -------------------------------------------------------------------------------- /test/interesting_methods_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class InterestingMethodsTest < Minitest::Test 4 | 5 | def test_that_it_has_a_version_number 6 | refute_nil ::InterestingMethods::VERSION 7 | end 8 | 9 | def test_class 10 | expected = [ 11 | :all, 12 | :report, 13 | :x, 14 | :y, 15 | :z 16 | ] 17 | assert_equal expected, Animal.im 18 | end 19 | 20 | def test_instance 21 | expected = [ 22 | :a, 23 | :b, 24 | :c, 25 | :sound, 26 | :speak, 27 | :speak_loudly, 28 | :type 29 | ] 30 | assert_equal expected, Animal.new('cow', 'moo').im 31 | end 32 | 33 | def test_inherited_class 34 | expected = [ 35 | :all, 36 | :report, 37 | :three_little_pigs, 38 | :x, 39 | :y, 40 | :z 41 | ] 42 | assert_equal expected, Pig.im 43 | end 44 | 45 | def test_inherited_instance 46 | expected = [ 47 | :a, 48 | :b, 49 | :c, 50 | :roll_around_in_mud, 51 | :sound, 52 | :speak, 53 | :speak_loudly, 54 | :type 55 | ] 56 | assert_equal expected, Pig.new('pig', 'oink').im 57 | end 58 | 59 | def test_extended_class 60 | expected = [ 61 | :all, 62 | :double_vision, 63 | :instance_blue, 64 | :instance_green, 65 | :instance_red, 66 | :report, 67 | :three_little_pigs, 68 | :x, 69 | :y, 70 | :z 71 | ] 72 | assert_equal expected, PigWithColoursExtended.im 73 | end 74 | 75 | def test_included_class 76 | expected = [ 77 | :a, 78 | :b, 79 | :c, 80 | :double_vision, 81 | :instance_blue, 82 | :instance_green, 83 | :instance_red, 84 | :roll_around_in_mud, 85 | :sound, 86 | :speak, 87 | :speak_loudly, 88 | :type 89 | ] 90 | assert_equal expected, PigWithColoursIncluded.new('pig', 'oink').im 91 | end 92 | 93 | def test_module 94 | expected = [ 95 | :class_blue, 96 | :class_green, 97 | :class_red, 98 | :double_vision, 99 | :instance_blue, 100 | :instance_green, 101 | :instance_red 102 | ] 103 | assert_equal expected, Colours.im 104 | end 105 | 106 | end 107 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Interesting Methods 2 | 3 | This gem lets you add `.im` to any object in Ruby to see the interesting methods. 4 | 5 | `.im` stands for 'Interesting Methods'. 6 | 7 | Without this gem, to find out an object's methods, you might try this: 8 | 9 | ```ruby 10 | MyClass.methods 11 | my_instance.methods 12 | ``` 13 | 14 | The problem? Because each of these statements show you all of the object's methods, including the inherited methods, it can be hard to find what you're after. 15 | 16 | --- 17 | 18 | To see only the interesting methods on an object, try one of these statements instead: 19 | 20 | ```ruby 21 | MyClass.methods - Object.methods 22 | my_instance.methods - Object.methods 23 | MyClass.singleton_methods(false) 24 | my_instance.instance_methods(false) 25 | MyModule.singleton_methods 26 | MyModule.instance_methods 27 | ``` 28 | 29 | --- 30 | 31 | The `interesting_methods` gem wraps the above techniques into a simple `.im` method that you can call on any object (class, instance, module) and display the methods you want: 32 | 33 | ``` 34 | MyClass.im # [:my_class_method_a, :my_class_method_b] 35 | my_instance.im # [:my_instance_method_a, :my_instance_method_b] 36 | MyModule.im # [:my_module_method_a, :my_module_method_b] 37 | ``` 38 | 39 | ## Installation 40 | 41 | First install the gem: 42 | 43 | ```shell 44 | gem install interesting_methods 45 | ``` 46 | 47 | Then create `irb` and `pry` *rc* files if they don't already exist: 48 | 49 | ```shell 50 | touch ~/.irbrc 51 | touch ~/.pryrc 52 | ``` 53 | 54 | Edit each of those `rc` files and add the following code: 55 | 56 | ```ruby 57 | if Gem::Specification.find_all_by_name('interesting_methods').any? 58 | require 'interesting_methods' 59 | end 60 | ``` 61 | 62 | You're all set up now! 63 | 64 | ## Usage 65 | 66 | Load up either `irb` or `pry` from your command line. 67 | Add `.im` to any object to see its interesting methods. 68 | 69 | ### Examples 70 | 71 | - (blog post with examples) 72 | - ('Method Driven Development' screencast using gem) 73 | 74 | ## Caveat 75 | 76 | This gem is not meant to be used in production as it monkey patches Ruby's core `Object` class. 77 | 78 | ## Development 79 | 80 | After checking out this repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. 81 | 82 | You can run `guard` for a continuous test runner. 83 | 84 | You can run `bin/console` for an interactive prompt that will allow you to experiment. 85 | 86 | ## Contributing 87 | 88 | Bug reports and pull requests are welcome on GitHub at https://github.com/seanlerner/interesting_methods. 89 | 90 | ## License 91 | 92 | The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). 93 | 94 | ## Credit 95 | 96 | Having `interesting_methods` available in your repl is something Ruby programmers have been doing for a while. I think I first came across it years ago in a stackoverflow post. Google `interesting_methods`and you'll find blog posts and dotfiles with similar functionality already implemented. AFAIK this is the first time its been packaged up in a gem. 97 | 98 | Sean Lerner
99 | http://smallcity.ca 100 | --------------------------------------------------------------------------------