📖 Scenario: Imagine you are exploring how Ruby treats everything as an object. This helps you understand how you can call methods on numbers, strings, and even true/false values.
🎯 Goal: You will create variables of different types and call methods on them to see how Ruby treats everything as an object.
📋 What You'll Learn
Create variables of different types: integer, string, and boolean
Create a variable called number with the value 10
Create a variable called greeting with the value "hello"
Create a variable called is_ruby_fun with the value true
Call the method .class on each variable to find out its class
Print the class of each variable
💡 Why This Matters
🌍 Real World
Understanding that everything is an object helps you write flexible and powerful Ruby code, since you can use methods on any value.
💼 Career
Many Ruby jobs require you to understand object-oriented concepts, and knowing that all data types are objects is fundamental.
Progress0 / 4 steps
1
Create variables of different types
Create a variable called number and set it to 10. Create a variable called greeting and set it to "hello". Create a variable called is_ruby_fun and set it to true.
Ruby
Hint
Use = to assign values to variables. Remember to put quotes around strings and use true for boolean.
2
Call the .class method on each variable
Call the method .class on the variable number and store the result in a variable called number_class. Do the same for greeting storing in greeting_class, and for is_ruby_fun storing in is_ruby_fun_class.
Ruby
Hint
Use the dot . to call the class method on each variable.
3
Print the class of each variable
Use puts to print the value of number_class, greeting_class, and is_ruby_fun_class each on its own line.
Ruby
Hint
Use puts to print each class variable on its own line.
4
Observe the output showing everything is an object
Run the program to see the output. It should print the class of each variable showing that numbers, strings, and booleans are all objects in Ruby.
Ruby
Hint
The output shows the class names: Integer, String, and TrueClass.
Practice
(1/5)
1. In Ruby, which of the following is not considered an object?
easy
A. Numbers like 5
B. Strings like "hello"
C. Booleans like true
D. Variables themselves
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 D
Quick 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
A. name.upcase
B. upcase(name)
C. name->upcase()
D. upcase.name()
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 variable name, calling upcase is done as name.upcase.
Final Answer:
name.upcase -> Option A
Quick 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
A. Integer
B. Fixnum
C. Numeric
D. Object
Solution
Step 1: Understand the class method
The class method 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 the Integer class (Fixnum and Bignum were unified).
Final Answer:
Integer -> Option A
Quick 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
A. Missing parentheses for method call
B. Using upcase! on a string literal causes error
C. The method upcase! returns nil if no changes made
D. Strings are not objects, so methods can't be called
Solution
Step 1: Understand upcase! behavior
The upcase! method modifies the string in place and returns nil if no changes were made.
Step 2: Analyze the code's output
Since "HELLO" is uppercase, upcase! makes no changes and returns nil, so puts prints nothing.
Step 3: Identify the error cause
The return value nil is printed when no mutation occurs.
Final Answer:
upcase! returns nil if no changes made -> Option C
Quick 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_s
hard
A. 42
B. Value is: 42
C. Value is: @value
D. Error: undefined method to_s
Solution
Step 1: Understand the class and method definitions
The class NumberWrapper stores a value and defines a to_s method that returns a string including the value.
Step 2: Analyze the to_s method call
The method returns the string "Value is: " plus the string version of @value, which is 42.
Step 3: Check the output of puts num.to_s
It prints the string returned by to_s, which is "Value is: 42".
Final Answer:
Value is: 42 -> Option B
Quick 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 unknownstrongwhile earning +1 Grip toward your certificate.