Play It Again, Ruby: Another Method Call Example

Just for good measure, let's walk through another method call in our violin family. What we're doing is getting a feel for how Ruby finds methods, or resolves method calls. It's worthwhile to gain some facility with this, since the strength of your program design will often depend on how you deploy inheritance.

Let's say we have created a Cello class:

class Cello < ViolinFamilyMember
  def standard_tuning
    %w{ c g d a }
  end
end

And now we create a Cello object

c = Cello.new

and we want to play the Bach Fifth Suite on it:

c.tune( %w{ c g d g }  )

What this does, as you can verify by looking back at the example, is set the instance variable @strings to the appropriate letter values.

Let's go over ...

Get Sams Teach Yourself Ruby in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.