Reading the Ruby documentation
Nobody knows every method by heart – not even Matz. What sets professionals apart is not how much they keep in their heads but how quickly they look things up. Ruby's official documentation lives at docs.ruby-lang.org: a page for every class with all its methods, explanations and plenty of examples. (In Japanese there is also the reference manual “Rurima”.)
How many methods does a string actually have? Ask Ruby itself:
"bacon".methods.lengthAround 200 – far too many to learn by heart. But you can search on purpose: grep filters the list by a pattern (more about such patterns in lesson 106). Which methods have to do with upper and lower case?
"bacon".methods.grep(/case/)Now you know what to look for. On the documentation page of String, for example, the entry for center looks like this (shortened):
center(size, pad_string = ' ') → new_string
Returns a centered copy of self.
If integer argument size is greater than the size (in characters)
of self, returns a new string of length size that is a copy of self,
centered and padded on one or both ends with pad_string:
'hello'.center(6) # => "hello "
'hello'.center(20, '-|') # => "-|-|-|-hello-|-|-|-|"
If size is less than or equal to the size of self, returns an
unpadded copy of self.The first line matters most. center is the name, the parentheses hold the arguments: size is required. pad_string = ' ' has a default value, a space, so you may leave it out. The arrow → tells you what comes back: a new string. Below come the explanation and examples – with # => the docs show the result, just like the notebook does. Try it:
puts "[" + "bacon".center(15) + "]"
puts "bacon".center(15, "*")
"bacon".center(3)The signs in the names tell you something, too. String#center with a hash sign is an instance method: you call it on a string, "bacon".center(15). File.read with a dot (sometimes written File::read) is a class method: you call it on the class itself. And Math::PI is a constant.
Always read what a method returns, too – that's where the surprises hide. The entry for upcase! says upcase!(mapping) → self or nil and explains: “Returns self if any changes are made, nil otherwise.” So nil when nothing changes:
p "bacon".upcase!
p "BACON".upcase!If you rely on upcase! always handing back a string, you'll sometimes get nil – a candidate for the NoMethodError from lesson 28. The docs would have told you.
One question remains: on which page is a method? Not every method an array can do is on the Array page! Many come from mixed-in modules or a parent class (lessons 44 and 45). Ruby tells you: method(:name).owner names the class or module that defines the method – and with it the right page of the docs:
p [1, 2, 3].method(:map).owner
p [1, 2, 3].method(:each_slice).owner
p "bacon".method(:between?).owner
5.method(:puts).ownerSo map is on the Array page, each_slice on Enumerable, between? on Comparable and puts on Kernel. And "bacon".respond_to?(:upcase) simply asks whether an object knows a method – true or false.
And gems? Their page on rubygems.org links to documentation and source code. Many gems explain themselves in their README on GitHub, and the method docs of nearly every gem are on rubydoc.info.
ri shows them, in IRB the command show_doc:$ ri String#center
$ ri Enumerable
$ irb
irb(main):001> show_doc Integer#digitsIf ri finds nothing, the docs weren't installed there – then use the website.For everyday use there's a shortcut: the sums up every topic of this course and links each one to the matching page of the docs.
Integer page has a method that splits a number into its digits – use it to compute the digit sum of 2026 (2 + 0 + 2 + 6) as digit_sum. 2. Enumerable has a method that cuts a list into pieces of equal size – with it, [1, 2, 3, 4, 5, 6] becomes pairs, that is [[1, 2], [3, 4], [5, 6]].# 1. The digit sum of 2026
# digit_sum = ...
# 2. The list in pairs
# pairs = ...