In Ruby, everything is treated as an object. This means all data and actions are objects, making the language simple and consistent.
Everything is an object mental model in Ruby
Introduction
Syntax
Ruby
# Example showing different objects 5.class # => Integer "hi".class # => String true.class # => TrueClass nil.class # => NilClass
Every value you use in Ruby is an object with its own class.
You can call methods on any object, even numbers and true/false.
Examples
Ruby
5.to_s "hello".upcase true.to_s
Ruby
nil.nil? 123.even? "abc".length
Sample Program
This program shows that numbers, strings, booleans, and nil are all objects. It also calls methods on them.
Ruby
puts 10.class puts "Ruby".class puts false.class puts nil.class puts 10.to_s puts "Ruby".upcase puts false.to_s
Important Notes
Remember, even simple things like numbers are objects in Ruby.
This makes Ruby very consistent and easy to learn.
Summary
Everything in Ruby is an object, including numbers, strings, and booleans.
You can call methods on any object, which makes Ruby simple and powerful.
This mental model helps you understand how Ruby works under the hood.
Practice
1. In Ruby, which of the following is
not considered an object?easy
Solution
Step 1: Understand Ruby's object model
In Ruby, everything you work with directly, like numbers, strings, and booleans, are objects.Step 2: Differentiate variables from objects
Variables are names or references pointing to objects, but they themselves are not objects.Final Answer:
Variables themselves are not objects -> Option DQuick Check:
Variables are references, not objects [OK]
Hint: Remember: variables point to objects, they aren't objects themselves [OK]
Common Mistakes:
- Thinking variables are objects
- Confusing object instances with variable names
- Assuming booleans are not objects
2. Which of the following is the correct way to call the method
upcase on a string object name in Ruby?easy
Solution
Step 1: Recall Ruby method call syntax
In Ruby, methods are called on objects using dot notation:object.method.Step 2: Apply to the string object
For the string variablename, callingupcaseis done asname.upcase.Final Answer:
name.upcase -> Option AQuick Check:
Method call syntax = object.method [OK]
Hint: Use dot (.) to call methods on objects in Ruby [OK]
Common Mistakes:
- Using function-style calls like upcase(name)
- Using arrow (->) notation from other languages
- Placing method after dot incorrectly
3. What will be the output of the following Ruby code?
num = 10 puts num.class
medium
Solution
Step 1: Understand the
Theclassmethodclassmethod returns the class of the object it is called on.Step 2: Identify the class of the number 10 in Ruby 3.12+
In modern Ruby versions, integers are instances of theIntegerclass (Fixnum and Bignum were unified).Final Answer:
Integer -> Option AQuick Check:
10.class = Integer [OK]
Hint: Use .class to find an object's type in Ruby [OK]
Common Mistakes:
- Choosing Fixnum (old Ruby versions)
- Confusing Numeric with Integer
- Thinking class returns Object always
4. The following Ruby code prints nothing. What is the problem?
text = "HELLO" puts text.upcase!
medium
Solution
Step 1: Understand
Theupcase!behaviorupcase!method modifies the string in place and returnsnilif no changes were made.Step 2: Analyze the code's output
Since "HELLO" is uppercase,upcase!makes no changes and returnsnil, soputsprints nothing.Step 3: Identify the error cause
The return valuenilis printed when no mutation occurs.Final Answer:
upcase!returns nil if no changes made -> Option CQuick Check:
Bang methods can return nil if no change [OK]
Hint: Bang (!) methods may return nil if no change happens [OK]
Common Mistakes:
- Thinking bang methods always return a string
- Assuming strings are not objects
- Missing that upcase! modifies in place
5. Given the code below, what will be the output?
class NumberWrapper
def initialize(value)
@value = value
end
def to_s
"Value is: #{@value.to_s}"
end
end
num = NumberWrapper.new(42)
puts num.to_shard
Solution
Step 1: Understand the class and method definitions
The classNumberWrapperstores a value and defines ato_smethod that returns a string including the value.Step 2: Analyze the
The method returns the string "Value is: " plus the string version ofto_smethod call@value, which is 42.Step 3: Check the output of
It prints the string returned byputs num.to_sto_s, which is "Value is: 42".Final Answer:
Value is: 42 -> Option BQuick Check:
Custom to_s returns formatted string [OK]
Hint: to_s returns string representation of any object [OK]
Common Mistakes:
- Expecting just the number without text
- Confusing instance variable with string literal
- Thinking to_s is undefined
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
