From: Ron Fox Date: 2007-12-18T21:30:01+09:00 Subject: Re: Newbie Question: What is a class for? One way to think of a class (a restricted definition to get you started) is as a data type and operations on that data type. Objects are variables of that data type. For example, consider a checking account. That has data (a balance), and operations: deposit, withdraw, write_check, balance etc. You may have several bank accounts in a bank class BankAccount def initialize who account_number initial_balance @name = who @account_number = account_number @balance = initial_balance end def deposit amount @balance += amount end def withdraw amount if amount <= @balance @balance -= amount else raise overdraft end end # and so on. end The class defines the data that makes up a checking account: @name - Name of the person who owns the account. @account_number - Account number of the account. @balance - Money currently in the account. initialize creates a new account (called when you do a BankAccount.new). deposit deposits funds in the account withdraw withdraws funds from the account. Each object of type BankAccount has its own private copies of @name, @account_number and @balance so: rons_account = BankAccount.new("Ron, 12345, 100) daves_account= BankAccount.new("Dave, 678, 150) Create two objects of type bank account with very different values of @name, @account_number and @balance. rons_account.withdraw 10 Affects ron's copy of @balance but not dave's. and so on. Matthias Borgeson wrote: > Hello all- > > I am a newbie to Ruby and have just recently gotten the hang of using > methods to hop in and out of the main body of my programs and am moving > to my next concept. > > I am now trying to grasp what a class isand what is is for. I understand > that there a bunch of different classes, but how does that help me? > > While I think of methods as little tools I can create to do things > whenever I want in the main program, I have no idea how a class used? I > have the Pickaxe and Chris Pine's book, but whle describing classes, > neither text really helps me understand what a class is and how it > functions in a ruby program. > > I looked through my C++ book to understand methods and how they are > anologous to functions, but now I am just confused on the concept. > > Any help or a point in the right direction would be appreciated > > Thanks in advance > > mfb > > >