The Make-Up of Ruby Classes: Some “What” and a Little “How”

We're going to start our detailed exploration of program organization by looking closely at classes: what goes into them and what comes out of them.

The Basics

You already know that a class definition includes zero or more methods, variables, and constants. So an example like this

class apple
  @@transient = 100 #class variable
  PERMANENT = 200 #class constant
  def say_something
    puts "Hello."
  end
end

holds no mystery for you. And that's most (though admittedly not all) of what you need to know to write any class in Ruby. (The other part of what you need to know, and eventually will know, has to do with writing class definitions inside class definitions, and a few other things in that vein.) ...

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.