Instance methods let objects do things or show information about themselves. They help organize code by grouping actions with the data they belong to.
Instance methods in Ruby
class ClassName def method_name # code here end end
Instance methods are defined inside a class using def.
You call instance methods on an object created from the class.
bark method for Dog objects.class Dog def bark puts "Woof!" end end
bark method, printing "Woof!".dog = Dog.new
dog.barkclass Person def greet(name) puts "Hello, #{name}!" end end
person = Person.new person.greet("Alice")
This program creates a Car object with make and model. The description method returns a string describing the car. We print that description.
class Car def initialize(make, model) @make = make @model = model end def description "This car is a #{@make} #{@model}." end end my_car = Car.new("Toyota", "Corolla") puts my_car.description
Instance methods can access instance variables like @make inside the object.
You must create an object (instance) of the class to use instance methods.
Use initialize method to set up initial values when creating an object.
Instance methods belong to objects and define what they can do.
Define instance methods inside a class using def.
Call instance methods on objects created from the class.
Practice
What is an instance method in Ruby?
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 AQuick Check:
Instance method = belongs to object [OK]
- Confusing instance methods with class methods
- Thinking instance methods run only once
- Believing instance methods cannot be called on objects
Which of the following is the correct way to define an instance method greet inside a Ruby class Person?
class Person ? end
Solution
Step 1: Identify instance method syntax
Instance methods are defined withdef method_nameinside the class withoutself..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 BQuick Check:
Instance method = def method_name [OK]
- Using self. which defines class methods
- Omitting semicolons or line breaks incorrectly
- Defining methods outside the class
What will be the output of the following Ruby code?
class Dog
def bark
'Woof!'
end
end
fido = Dog.new
puts fido.barkSolution
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 CQuick Check:
Calling instance method returns 'Woof!' [OK]
- Expecting method name instead of return value
- Confusing class name with output
- Thinking method is undefined
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()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, sokitty.meow()is valid.Final Answer:
No error; code works as is -> Option DQuick Check:
Parentheses optional in method calls [OK]
- Thinking parentheses cause errors
- Confusing instance and class methods
- Adding unnecessary return before puts
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')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 theupcasemethod.Final Answer:
JANE DOE -> Option AQuick Check:
full_name returns uppercase full name [OK]
- Ignoring upcase method effect
- Expecting original case output
- Confusing string quotes in output
Ready to prove it?
Pass the test and this pattern moves unknown
