Interesting Methods Gem
I’ve just published a simple new gem called interesting_methods
.
After you install and set this up, from within irb
and pry
, you can type:
MyClass.im
# &
my_instance.im
to see all the interesting methods available to you on those objects.
To Set Up
First install the gem:
gem install interesting_methods
Then create irb and pry rc files if they don’t already exist:
touch ~/.irbrc
touch ~/.pryrc
Edit each of those rc
files and add the following code:
if Gem::Specification.find_all_by_name('interesting_methods').any?
require 'interesting_methods'
end
You’re all set up now!
To Use
Load up either irb
or pry
from your command line.
Add .im
to any object to see its interesting methods.
Example
Load up pry:
$ pry
You should be at the pry prompt now:
pry >
Paste the following code into pry:
class Animal
@@all = []
attr_reader :type, :sound
def initialize(type, sound)
@type = type
@sound = sound
@@all << self
end
def speak_loudly
puts @sound.upcase
end
def speak
puts @sound
end
def self.all
@@all
end
def self.report
puts "#{'Type'.rjust(15)} #{'Sound'.rjust(15)}"
puts "#{'----'.rjust(15)} #{'-----'.rjust(15)}"
all.each do |animal|
puts "#{animal.type.rjust(15)} #{animal.sound.rjust(15)}"
end
end
end
cow = Animal.new('cow', 'moo')
Now check out the methods on both Animal
and cow
(an instance of Animal
):
pry > Animal.im
=> [:all, :report]
pry > cow.im
=> [:sound, :speak, :speak_loudly, :type]
Repo
You can find the repo at https://github.com/seanlerner/interesting_methods
In my next post, you’ll find a screencast that illustrates a technique I call, Method Driven Development using the gem.
Enjoy!