0
0
Rubyprogramming~20 mins

Why everything is an object in Ruby - See It in Action

Choose your learning style9 modes available
Why Everything is an Object in Ruby
📖 Scenario: Imagine you are learning Ruby and want to understand how Ruby treats everything as an object. This helps you see how flexible and consistent Ruby is when working with data and methods.
🎯 Goal: You will create simple Ruby variables of different types and check their class to see that all are objects. This will help you understand the concept that everything in Ruby is an object.
📋 What You'll Learn
Create variables of different types: integer, string, array, and a custom object
Create a variable called number with the value 10
Create a variable called text with the value "Hello"
Create a variable called list with the value [1, 2, 3]
Create a simple class called Person with an attribute name
Create an instance of Person called person with the name "Alice"
Print the class of each variable using .class method
💡 Why This Matters
🌍 Real World
Understanding that everything is an object helps you write clear and consistent Ruby code, making it easier to use libraries and frameworks.
💼 Career
Many Ruby jobs require strong knowledge of Ruby's object model to build and maintain web applications, scripts, and tools.
Progress0 / 4 steps
1
Create variables of different types
Create a variable called number with the value 10, a variable called text with the value "Hello", and a variable called list with the value [1, 2, 3].
Ruby
Need a hint?

Use simple assignment to create variables with the exact names and values.

2
Create a simple class and an object
Create a class called Person with an initialize method that takes a name parameter and sets it to an instance variable. Then create an instance of Person called person with the name "Alice".
Ruby
Need a hint?

Define a class with class and def initialize(name). Use @name = name to store the name.

3
Check the class of each variable
Use puts to print the class of number, text, list, and person by calling the .class method on each.
Ruby
Need a hint?

Use puts variable.class to print the class of each variable.

4
Run and observe the output
Run the program and observe the output showing the class of each variable. This proves that everything in Ruby is an object.
Ruby
Need a hint?

The output should show the class names: Integer, String, Array, and Person.