O'Reilly logo

Sams Teach Yourself Ruby in 21 Days by Mark Slagell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required