My First Ruby Gem Part 4: Reap What You Sow

(If you haven’t already, please read Part 1, Part 2, and Part 3)

What I Built

Below is a description of RubyTutor and how to use it. Click Here to read the full documentation on GitHub

RubyTutor was created to be an IRB tool to help beginners become better acquainted with the Ruby language. It has 4 class methods:

  • RubyTutor.explain_full(object)
  • RubyTutor.explain(object)
  • RubyTutor.describe(object)
  • RubyTutor.available_methods(object, filter)

explain_full

RubyTutor.explain_full accepts any object as an argument and outputs general information about it, as well as a description of it’s class.

Here’s a sample output:

RubyTutor.explain_full 'string'
# >> Instance of Class: String
# >> Value: string
# >> Length: 6
# >> Mutable? Yes
# >> Object ID: 70179170680460
# >> Inherits From: Comparable, Object, Kernel, BasicObject
# >>
# >> Description:
# >> This object is an instance of the String class.
# >> A String object is an expression that can hold
# >> letters, numbers and all sorts of different characters,
# >> as long as they are surrounded by single ('')
# >> or double ("") quotes.
# >>
# >> Type RubyTutor.available_methods String
# >> to see all of the methods available.

For newcomers that aren’t familiar with Object-Oriented Programming(OOP), Instance of Class, Object ID, and Inherits From might not make sense to you. I won’t get into the specifics of OOP here, but know that objects are like people. They contain states (length, value) and behaviors (methods). Everything in Ruby is an object, from strings to even nil. This means they all have states that be accessed and behaviors that can be used.

explain and describe

RubyTutor.explain and RubyTutor.describe separately output the two parts of RubyTutor.explain_full — just incase you don't want to get the entire spiel.

RubyTutor.explain 'string'
# >> Instance of Class: String
# >> Value: string
# >> Length: 6
# >> Mutable? Yes
# >> Object ID: 70179170680460
# >> Inherits From: Comparable, Object, Kernel, BasicObject
RubyTutor.describe 'string'
# >> Description:
# >> This object is an instance of the String class.
# >> A String object is an expression that can hold
# >> letters, numbers and all sorts of different characters,
# >> as long as they are surrounded by single ('')
# >> or double ("") quotes.
# >>
# >> Type RubyTutor.available_methods String
# >> to see all of the methods available.

As of right now, RubyTutor will provide a description for all of the basic classes such as Fixnum, Float, String, etc. I will continue to update description support and improve the descriptions based on any feedback.

Also, to provide credit where it is due, most of the descriptions were copied from the Ruby Docs and edited to be easier to understand for beginners.

available_methods

The last method, RubyTutor.available_methods, was more of a fix than an original feature. Whenever you call the .methods method on any object, you get a huge jumbled array in return that is hard to read. I wanted it provide a nicer layout and a way of filtering the results.

.available_methods accepts an object and an optional filter. The filter must be a string and it must match the beginning of the method name.

For example, if you pass in 'to' as a filter, it will return all the methods that begin with 'to’ — not all the methods that have 'to' in the name. Any filter that isn't a string will get ignored and all the available methods will be outputted.

RubyTutor.available_methods BasicObject
# >> Available Methods:
# >> !
# >> !=
# >> !~
# >> <
# >> <=
# >> <=>
# >> ==
# >> ===
# >> =~
# >> >
# >> >=
# >> __id__
# >> __send__
# >> allocate
# >> ancestors
# >> autoload
# >> autoload?
# >> class
# >> class_eval
# >> class_exec
# >> class_variable_defined?
# >> class_variable_get
# >> class_variable_set
# >> class_variables
#...(truncated for brevity)
RubyTutor.available_methods BasicObject, 'al'
# >> Available Methods:
# >> allocate

So that’s RubyTutor in a nutshell. I tried to make a good test suite for it and catch any mistakes or bugs, but if you find something please let me know.

Also, if you want to add to it or make new features to it, go ahead! Fork it, clone it and add to it, my friend.

I hope this inspires others to tackle projects they once thought were out of their reach. And even if you do fail, those are the times we learn the most. So what have you got to lose?

Happy Programming!

References