An instance method is a function defined inside a class that can be called on objects (instances) created from that class. It operates on the data contained in the specific object.
Click to reveal answer
beginner
How do you define an instance method in a Ruby class?
You define an instance method inside a class using the <code>def</code> keyword followed by the method name. For example:<br><pre>class Dog
def bark
puts "Woof!"
end
end</pre>
Click to reveal answer
beginner
How do you call an instance method in Ruby?
First, create an object (instance) of the class, then call the method on that object using dot notation. For example:
dog = Dog.new
dog.bark # Calls the bark method
Click to reveal answer
intermediate
What is the role of self inside an instance method?
Inside an instance method, self refers to the current object (instance) on which the method was called. It helps access or modify the object's data.
Click to reveal answer
intermediate
Can instance methods access instance variables? How?
Yes. Instance methods can read or change instance variables using the @ symbol. For example:
def name
@name
end
accesses the @name variable of the object.
Click to reveal answer
How do you define an instance method in Ruby?
AUsing <code>class method_name</code>
BUsing <code>def self.method_name</code> inside a class
CUsing <code>function method_name()</code>
DUsing <code>def method_name</code> inside a class
✗ Incorrect
Instance methods are defined with def method_name inside a class. Option B defines a class method.
What does self refer to inside an instance method?
AThe current object (instance)
BA global variable
CThe class itself
DThe method name
✗ Incorrect
self inside an instance method refers to the object that called the method.
How do you call an instance method named greet on an object person?
A<code>person.greet</code>
B<code>call greet on person</code>
C<code>Person.greet</code>
D<code>greet.person</code>
✗ Incorrect
You call instance methods on objects using dot notation: person.greet.
Can instance methods access instance variables?
ANo, only class methods can
BOnly if they are global variables
CYes, using the <code>@</code> symbol
DOnly if passed as arguments
✗ Incorrect
Instance methods can access instance variables using the @ symbol, like @name.
Which of these is an instance method definition?
Adef self.run
Bdef run
Cdef Class.run
Ddef @run
✗ Incorrect
Instance methods are defined with def method_name. Option A defines a class method.
Explain what an instance method is and how it is used in Ruby.
Think about how methods belong to objects.
You got /4 concepts.
Describe how self works inside an instance method and why it is useful.
Consider what 'self' means when you call a method on an object.
You got /3 concepts.
Practice
(1/5)
1.
What is an instance method in Ruby?
easy
A. A method that belongs to an object created from a class
B. A method that belongs to the class itself, not objects
C. A method that runs only once when the program starts
D. A method that cannot be called on any object
Solution
Step 1: Understand what instance methods are
Instance methods are defined inside a class and belong to objects created from that class.
Step 2: Differentiate from class methods
Class methods belong to the class itself, not to individual objects.
Final Answer:
A method that belongs to an object created from a class -> Option A
Quick Check:
Instance method = belongs to object [OK]
Hint: Instance methods belong to objects, not the class itself [OK]
Common Mistakes:
Confusing instance methods with class methods
Thinking instance methods run only once
Believing instance methods cannot be called on objects
2.
Which of the following is the correct way to define an instance method greet inside a Ruby class Person?
class Person
?
end
easy
A. def self.greet; puts 'Hello'; end
B. def greet; puts 'Hello'; end
C. def Person.greet; puts 'Hello'; end
D. def greet() puts 'Hello' end
Solution
Step 1: Identify instance method syntax
Instance methods are defined with def method_name inside the class without self..
Step 2: Check options
def greet; puts 'Hello'; end uses correct syntax. def self.greet; puts 'Hello'; end and def Person.greet; puts 'Hello'; end define class methods. def greet() puts 'Hello' end misses semicolons or line breaks, causing syntax issues.
Final Answer:
def greet; puts 'Hello'; end -> Option B
Quick Check:
Instance method = def method_name [OK]
Hint: Instance methods use def without self. [OK]
Common Mistakes:
Using self. which defines class methods
Omitting semicolons or line breaks incorrectly
Defining methods outside the class
3.
What will be the output of the following Ruby code?
class Dog
def bark
'Woof!'
end
end
fido = Dog.new
puts fido.bark
medium
A. Dog
B. bark
C. Woof!
D. NoMethodError
Solution
Step 1: Understand the class and method
The class Dog has an instance method bark that returns the string 'Woof!'.
Step 2: Trace the method call
An object fido is created from Dog, then fido.bark calls the bark method, returning 'Woof!'.
Final Answer:
Woof! -> Option C
Quick Check:
Calling instance method returns 'Woof!' [OK]
Hint: Instance method returns its string when called on object [OK]
Common Mistakes:
Expecting method name instead of return value
Confusing class name with output
Thinking method is undefined
4.
Find the error in this Ruby code and select the fix that will make it work:
class Cat
def meow
puts 'Meow!'
end
end
kitty = Cat.new
kitty.meow()
medium
A. Remove parentheses from kitty.meow() to kitty.meow
B. Change def meow to def self.meow
C. Add return before puts in method
D. No error; code works as is
Solution
Step 1: Check method definition and call
The method meow is defined correctly as an instance method and called on an object.
Step 2: Verify syntax and usage
Using parentheses in method call is allowed in Ruby, so kitty.meow() is valid.
Final Answer:
No error; code works as is -> Option D
Quick Check:
Parentheses optional in method calls [OK]
Hint: Parentheses optional in Ruby method calls [OK]
Common Mistakes:
Thinking parentheses cause errors
Confusing instance and class methods
Adding unnecessary return before puts
5.
Given the class below, what will puts user.full_name print?
class User
def initialize(first, last)
@first_name = first
@last_name = last
end
def full_name
"#{@first_name} #{@last_name}".upcase
end
end
user = User.new('Jane', 'Doe')
hard
A. JANE DOE
B. Error: undefined method full_name
C. "Jane Doe"
D. Jane Doe
Solution
Step 1: Understand instance variables and method
The initialize method sets @first_name and @last_name. The full_name method returns their combination in uppercase.
Step 2: Trace the method call
Calling user.full_name returns "JANE DOE" because of the upcase method.
Final Answer:
JANE DOE -> Option A
Quick Check:
full_name returns uppercase full name [OK]
Hint: String interpolation with upcase changes output to uppercase [OK]
Common Mistakes:
Ignoring upcase method effect
Expecting original case output
Confusing string quotes in output
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.