Bird
Raised Fist0
Rubyprogramming~20 mins

Instance methods in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Instance Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of calling an instance method
What is the output of this Ruby code when calling the greet method on an instance of the Person class?
Ruby
class Person
  def initialize(name)
    @name = name
  end

  def greet
    "Hello, #{@name}!"
  end
end

person = Person.new("Alice")
puts person.greet
AHello, Alice!
BHello, @name!
Cperson.greet
DError: undefined method greet
Attempts:
2 left
💡 Hint
Instance methods can access instance variables using @variable_name.
❓ Predict Output
intermediate
2:00remaining
Instance method modifying state
What will be the output after running this Ruby code?
Ruby
class Counter
  def initialize
    @count = 0
  end

  def increment
    @count += 1
  end

  def current
    @count
  end
end

counter = Counter.new
counter.increment
counter.increment
puts counter.current
A2
B0
C1
DError: undefined method current
Attempts:
2 left
💡 Hint
Each call to increment adds 1 to @count.
🔧 Debug
advanced
2:00remaining
Why does this instance method raise an error?
This Ruby code raises an error when calling display. What is the cause?
Ruby
class Book
  def initialize(title)
    title = title
  end

  def display
    puts "Title: #{title}"
  end
end

book = Book.new("Ruby Guide")
book.display
AMissing return statement in initialize method
BSyntax error in initialize method
CUndefined local variable or method `title` in display method
DCannot call puts inside instance method
Attempts:
2 left
💡 Hint
Instance variables start with @ and are needed to keep data between methods.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this instance method
Which option correctly identifies the syntax error in this Ruby code?
Ruby
class Calculator
  def add(a, b)
    return a + b
  end

  def multiply(a, b)
    a * b
  # missing end here
end

calc = Calculator.new
puts calc.add(2, 3)
AMissing parentheses in method call add(2, 3)
BMissing <code>end</code> keyword for multiply method
CIncorrect use of return in add method
DCannot create instance of Calculator without arguments
Attempts:
2 left
💡 Hint
Every method definition must be closed with end.
🚀 Application
expert
3:00remaining
How many times is the instance method called?
Consider this Ruby code. How many times is the log instance method called during execution?
Ruby
class Logger
  def initialize
    @count = 0
  end

  def log(message)
    @count += 1
    puts "Log ##{@count}: #{message}"
  end

  def repeat_log(times, message)
    times.times { log(message) }
  end
end

logger = Logger.new
logger.log("Start")
logger.repeat_log(3, "Repeat")
logger.log("End")
A6
B4
C3
D5
Attempts:
2 left
💡 Hint
Count each explicit call to log, including inside loops.

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

  1. Step 1: Understand what instance methods are

    Instance methods are defined inside a class and belong to objects created from that class.
  2. Step 2: Differentiate from class methods

    Class methods belong to the class itself, not to individual objects.
  3. Final Answer:

    A method that belongs to an object created from a class -> Option A
  4. 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

  1. Step 1: Identify instance method syntax

    Instance methods are defined with def method_name inside the class without self..
  2. 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.
  3. Final Answer:

    def greet; puts 'Hello'; end -> Option B
  4. 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

  1. Step 1: Understand the class and method

    The class Dog has an instance method bark that returns the string 'Woof!'.
  2. Step 2: Trace the method call

    An object fido is created from Dog, then fido.bark calls the bark method, returning 'Woof!'.
  3. Final Answer:

    Woof! -> Option C
  4. 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

  1. Step 1: Check method definition and call

    The method meow is defined correctly as an instance method and called on an object.
  2. Step 2: Verify syntax and usage

    Using parentheses in method call is allowed in Ruby, so kitty.meow() is valid.
  3. Final Answer:

    No error; code works as is -> Option D
  4. 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

  1. 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.
  2. Step 2: Trace the method call

    Calling user.full_name returns "JANE DOE" because of the upcase method.
  3. Final Answer:

    JANE DOE -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard