An instance variable in Ruby is a variable that belongs to a specific object (instance) of a class. It starts with the @ symbol and stores data unique to that object.
Click to reveal answer
beginner
How do you declare an instance variable inside a Ruby class?
You declare an instance variable by prefixing the variable name with @ inside any method of the class, usually inside the initialize method.
Click to reveal answer
beginner
Why do instance variables start with @ in Ruby?
The @ symbol tells Ruby that the variable is tied to a specific object instance, not a local or global variable. It helps Ruby keep track of data for each object separately.
Click to reveal answer
intermediate
Can instance variables be accessed directly outside the class?
No, instance variables are private to the object. To access them outside the class, you need to use methods like getters or attr_reader.
Click to reveal answer
beginner
Example: What does this code print?
<pre>class Dog
def initialize(name)
@name = name
end
def speak
"Woof! My name is #{@name}"
end
end
dog = Dog.new("Buddy")
puts dog.speak</pre>
It prints: Woof! My name is Buddy
The @name instance variable stores the dog's name and is used inside the speak method.
Click to reveal answer
What symbol is used to start an instance variable in Ruby?
A@
B$
C#
D&
✗ Incorrect
Instance variables in Ruby always start with the @ symbol.
Where are instance variables typically declared in a Ruby class?
AOutside the class
BInside the initialize method
CInside a global method
DIn a local variable
✗ Incorrect
Instance variables are usually set inside the initialize method to store object-specific data.
Can you access an instance variable directly from outside the object?
ANo, you need a method to access it
BYes, always
COnly if it starts with $
DOnly inside a module
✗ Incorrect
Instance variables are private to the object; you must use methods like getters to access them.
What does @name represent inside a Ruby class?
AA constant
BA local variable
CA global variable
DAn instance variable
✗ Incorrect
The @ symbol means the variable is an instance variable tied to the object.
If you create two objects from the same class, do they share the same instance variables?
AOnly if they are global
BYes, instance variables are shared
CNo, each object has its own instance variables
DOnly if declared outside methods
✗ Incorrect
Each object has its own copy of instance variables, so data is unique per object.
Explain what an instance variable is in Ruby and why it starts with @.
Think about how Ruby keeps data unique for each object.
You got /4 concepts.
Describe how you would use instance variables to store and access a person's name in a Ruby class.
Consider how to keep the name inside the object and show it outside.
You got /4 concepts.
Practice
(1/5)
1. What does an instance variable in Ruby always start with?
easy
A. An at symbol (@)
B. A dollar sign ($)
C. A hash symbol (#)
D. A percent sign (%)
Solution
Step 1: Recall Ruby instance variable syntax
Instance variables in Ruby always begin with the at symbol (@) to distinguish them from local variables.
Step 2: Identify the correct symbol
Among the options, only the at symbol (@) is used to start instance variables.
Final Answer:
An at symbol (@) -> Option A
Quick Check:
Instance variable starts with @ [OK]
Hint: Instance variables always start with @ in Ruby [OK]
Common Mistakes:
Confusing instance variables with global variables ($)
Using local variable syntax without @
Thinking instance variables start with # or %
2. Which of the following is the correct way to define an instance variable inside a Ruby class?
In Ruby, instance variables are assigned by writing @variable_name = value inside methods or constructors.
Step 2: Check each option
@name = 'Alice' correctly assigns an instance variable. The other options use invalid syntax.
Final Answer:
@name = 'Alice' -> Option B
Quick Check:
Assign instance variable with @name = value [OK]
Hint: Assign instance variables with @name = value inside methods [OK]
Common Mistakes:
Adding var keyword (not used in Ruby)
Placing @ inside quotes
Using def keyword incorrectly
3. What will be the output of this Ruby code?
class Person
def initialize(name)
@name = name
end
def greet
"Hello, #{@name}!"
end
end
p = Person.new('Bob')
puts p.greet
medium
A. Error: undefined variable
B. Hello, @name!
C. Hello, name!
D. Hello, Bob!
Solution
Step 1: Understand instance variable assignment in initialize
The initialize method sets @name to 'Bob' when Person.new('Bob') is called.
Step 2: Evaluate greet method output
The greet method returns a string with #{@name} replaced by the instance variable value 'Bob'.
Final Answer:
Hello, Bob! -> Option D
Quick Check:
Instance variable @name holds 'Bob' so output is Hello, Bob! [OK]
Hint: Instance variables keep data per object, used inside methods with @ [OK]
Common Mistakes:
Expecting literal '@name' in output
Confusing local and instance variables
Forgetting to use @ in string interpolation
4. Identify the error in this Ruby class code:
class Car
def initialize(model)
name = model
end
def show
puts @name
end
end
car = Car.new('Tesla')
car.show
medium
A. Syntax error in initialize method
B. Method show is missing return statement
C. Instance variable @name is not assigned, so nil is printed
D. Cannot call puts inside method
Solution
Step 1: Check variable assignment in initialize
The code assigns local variable name = model, not instance variable @name.
Step 2: Understand effect on show method
Since @name is never set, @name is nil, so puts prints an empty line (nil).
Final Answer:
Instance variable @name is not assigned, so nil is printed -> Option C
Quick Check:
Local variable assigned, not instance variable @name [OK]
Hint: Use @ to assign instance variables, not plain variable names [OK]
Common Mistakes:
Assigning local variable instead of instance variable
Expecting puts to fail without return
Thinking syntax error occurs here
5. You want to create a Ruby class Book that stores a title and author as instance variables. You also want a method description that returns "Title by Author". Which code correctly implements this?
hard
A. class Book
def initialize(title, author)
@title = title
@author = author
end
def description
"#{@title} by #{@author}"
end
end
B. class Book
def initialize(title, author)
title = @title
author = @author
end
def description
"#{title} by #{author}"
end
end
C. class Book
def initialize(title, author)
@title = title
@author = author
end
def description
"title by author"
end
end
D. class Book
def initialize(title, author)
@title = title
@author = author
end
def description
"#{title} by #{author}"
end
end
Solution
Step 1: Assign instance variables correctly in initialize
class Book
def initialize(title, author)
@title = title
@author = author
end
def description
"#{@title} by #{@author}"
end
end assigns @title and @author from parameters correctly. class Book
def initialize(title, author)
title = @title
author = @author
end
def description
"#{title} by #{author}"
end
end reverses assignment. The remaining options misuse variables in description.
Step 2: Use instance variables in description method
class Book
def initialize(title, author)
@title = title
@author = author
end
def description
"#{@title} by #{@author}"
end
end uses #{@title} and #{@author} inside string interpolation, correctly accessing instance variables.
Final Answer:
class Book
def initialize(title, author)
@title = title
@author = author
end
def description
"#{@title} by #{@author}"
end
end -> Option A
Quick Check:
Assign and use @variables inside methods for correct output [OK]
Hint: Assign and use instance variables with @ inside all methods [OK]
Common Mistakes:
Assigning variables backwards (param = @var)
Using local variables instead of instance variables in methods
Returning literal strings without interpolation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.