June 2007
Intermediate to advanced
294 pages
8h 6m
English
In the previous script, we added new methods to allow explicit Boolean casting to every Object in all of Ruby. In this example, we create a new method that is a slight variation on a method that already exists. In this case, we’re altering the way that Arrays can represent themselves as Strings.
When we talk about lists in natural speaking, we often separate the last item from the item before it with the word and. This is not how Ruby handles Arrays by default. Let’s verify that in irb:
irb(main):001:0> a = [0, 1, 2]
=> [0, 1, 2]
irb(main):002:0> a.join(' ') The join Method
=> "0 1 2"
irb(main):003:0> a.join(', ')
=> "0, 1, 2"
irb(main):004:0> a.join('')
=> "012"We’re creating a variant of the method join, which ...
Read now
Unlock full access