Clarifying classes, objects and variables; or how objects work in Ruby

Christopher Agnus 🐟
Zero to Code
Published in
2 min readMar 7, 2018

How do we create classes and objects in Ruby? Ruby is an object-oriented programming language. And everything in Ruby is an object that has been generated directly or indirectly from a class.

The best way to conceptualise this is giving ourselves a problem to solve. Let’s say we are running a bookstore. Every week we do stock control. Bar-code scanners are used to record every book on the shelves. The scanner generates a CSV file containing one row for each book scanned. The row contains the book’s ISBN and price, like this:

“Date”, “ISBN”, “Price”

“2013–04–12”,”978–1–9343561–0–4",39.45
“2013–04–13”,”978–1–9343561–6–6",45.67
“2013–04–14”,”978–1–9343560–7–4",36.95

When designing OO systems, work out the things you are dealing with. Typically, each type of thing becomes a class in your final program, and the things themselves are instances of these classes.

Take the CSV files and work out how many of each title we have and the total list price of the books in stock.

Each instance will represent a row of data and the collection of all these objects will represent all the data we’ve captured.

So, here’s how we will create this:

class BookInStockdef initialize(isbn, price)@isbn = isbn@price = Float(price)enddef to_s #render objects as a string"ISBN: #{@isbn}, price:#{@price}"endend
b1 = BookInStock.new("isbn1", 39.45)puts b1 b2 = BookInStock.new("isbn2", 45.67)puts b2 b3 = BookInStock.new("isbn3", "36.95")puts b3

Explanation:

Create the class BookInStock (class names start with an uppercase letter)

Create new instance of the class using .new ie. a_book = BookInStock.new

The instance objects don’t hold any information yet so we need to provide the objects with an initialize method. This lets us set the state of each object as it is constructed. We store this state in instance variables (instance variables start with an @ sign).

Note that at first @isbn and isbn may look related because they have the same name. They don’t. The former is an instance variable, and the ‘@’ symbol is part of its name.

For the class BookInStock, the initialize method takes two parameters. The parameters act like local variables within the method, so they start with a lowercase letter. But as local variables, they evaporate once the initialize method returns so we need to transfer them into an instance variable.

The Float method takes its argument and converts it to a floating-point number to validate whether the object is valid.

puts vs p: puts writes strings to your program’s output, however, without to_s it prints a whole lot inside #<..>. p is the same as printing the value of .inspect.

When the puts method is run, it calls to_s in that object to get its string representation (makes something easy to display).

If this post helped you, give me a follow+ hold onto the claps button!

--

--

Christopher Agnus 🐟
Zero to Code

Hi, I’m Christopher (Agnus) Lam. I write about startups, entrepreneurship and marketing.