Create great Ruby programs fast with self-reliance
“I loooove buggy, bloated, incomplete software that takes a lot of time and money to create” - Nobody
Self-reliance doesn’t mean you need to go it alone. You’ve got the three amigos on the CLI to help you out:
ruby- run ruby programs via the command line
irb- run interactive ruby via the command line
ri- ruby documentation via the command line
Restrict yourself to using only these three tools - no Google, no Stack Overflow, no outside help.
You can work this way all the time, or just try it out for five minutes per day.
Experiment, and play. Tell people about what you’ve learned.
To understand Ruby programs, you need to track the value of
You can view the text representation of any object by typing
puts self in
You can inspect any object by typing
Keep it classy
Objects derive their behavior from their class. Since
self returns an object, you can find that object’s class using
Because classes in Ruby are also objects, you can always do
p self.class although that’s usually less interesting, because in both cases it just prints the class name.
Go ri a book
ri command-line program gives you instant access to documentation for the entire Ruby core and standard libraries.
ri from the command-line and it prompts you for a class or method name. Try typing in a class from one of the
self.class calls from before.
When you run
ri interactively from the command-line, it offers tab-completion. If you want to know of parts of Ruby that deal with strings… type
String at the prompt and hit tab a couple times. You’ll get a few classes:
Hit enter for a matching class name and you get a complete class reference, with modules it includes, a description, and a list of class and instance methods.
You can dig deeper and read the documentation for any method: you just need to know the syntax to indicate class and instance methods.
String.try_convert means the class method named
try_convert on the
String#capitalize means an instance method called
capitalize on the class
String, making it available to all instances of class
String.try_convert at the
ri prompt to read the class method documentation, and
String#capitalize to look at the instance method.
The rest is up to you
The three command-line tools let you interact completely with Ruby. Anything you see that looks like Ruby, type it into
ri to try to find some documentation.
By tracking the value of
self and the instance’s class, you can intelligently query the
ri documentation whether you need to search for how to do something, or you don’t understand an error.
(You’ve gotten a
NoMethodError in your code before, right? Try typing that in to
ri and you’ll get a complete explanation!)
Go forth and create as many programs as you want, fellow coder.
You will struggle at times, but you have all the tools you need to learn – you just need to practice!
Do it, and help others do it.