0
0
Rubyprogramming~15 mins

Class declaration syntax in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Class declaration syntax
📖 Scenario: You want to organize information about a simple object in your program. Using a class helps you group related data and actions together.
🎯 Goal: Create a Ruby class named Car with a method to show its model.
📋 What You'll Learn
Create a class named Car
Add an initialize method that takes a model parameter
Store the model in an instance variable
Add a method show_model that returns the model
Create an object of class Car with model "Toyota"
Print the result of calling show_model on the object
💡 Why This Matters
🌍 Real World
Classes help organize data and behavior for real-world objects like cars, users, or products in software.
💼 Career
Understanding class syntax is essential for Ruby developers building applications with reusable and organized code.
Progress0 / 4 steps
1
Create the Car class
Write a class declaration named Car with an empty body.
Ruby
Need a hint?

Use class Car to start and end to finish the class.

2
Add the initialize method
Inside the Car class, write an initialize method that takes a parameter model and stores it in an instance variable @model.
Ruby
Need a hint?

Use def initialize(model) and assign @model = model.

3
Add the show_model method
Inside the Car class, add a method named show_model that returns the instance variable @model.
Ruby
Need a hint?

Define def show_model and return @model.

4
Create an object and print the model
Create an object my_car of class Car with model "Toyota". Then print the result of calling show_model on my_car.
Ruby
Need a hint?

Create the object with Car.new("Toyota") and print with puts.