0
0
Rubyprogramming~20 mins

Rubocop for linting in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Rubocop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of Rubocop when analyzing this Ruby code?

Given the following Ruby code, what output will Rubocop produce regarding style offenses?

Ruby
def greet(name)
  puts "Hello, #{name}"
end

greet("Alice")
ANo offenses detected
BStyle/MethodDefParentheses: Use parentheses when defining a method
CLint/UselessAssignment: Useless assignment to variable 'name'
DStyle/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols.
Attempts:
2 left
💡 Hint

Look for string style offenses in the code.

🧠 Conceptual
intermediate
1:30remaining
Which Rubocop cop detects unused variables?

Which Rubocop cop is responsible for detecting variables that are assigned but never used in Ruby code?

ALint/UselessAssignment
BStyle/UnusedVariable
CLint/UnusedMethodArgument
DStyle/UnusedBlockArgument
Attempts:
2 left
💡 Hint

Think about the cop that warns about useless assignments.

🔧 Debug
advanced
2:30remaining
Why does Rubocop raise an error on this code snippet?

Consider this Ruby code snippet. Rubocop raises an error. What is the cause?

Ruby
class Person
  def initialize(name)
    @name = name
  end

  def greet
    puts "Hello, #{name}"
  end
end
ALint/UselessInstanceVariable because @name is never used
BNameError because 'name' is undefined in greet method
CNameError because @name is not accessible in greet method
DNo offenses detected
Attempts:
2 left
💡 Hint

Check variable scope inside methods.

📝 Syntax
advanced
2:00remaining
Which option causes a Rubocop syntax offense?

Which of the following Ruby code snippets will cause Rubocop to report a syntax offense?

A
def add(a, b)
  a + b
  end
B
def add a, b
  a + b
end
C
def add(a, b)
  return a + b
end
D
def add(a, b)
  a + b
end
Attempts:
2 left
💡 Hint

Look for indentation and end keyword placement.

🚀 Application
expert
3:00remaining
How many style offenses will Rubocop detect in this code?

Analyze the following Ruby code. How many style offenses will Rubocop report?

Ruby
class Calculator
  def add(a,b)
    a + b
  end

  def subtract(a, b)
    a - b
  end

  def multiply(a, b)
    a * b
  end

  def divide(a, b)
    return nil if b == 0
    a / b
  end
end
A1
B3
C2
D4
Attempts:
2 left
💡 Hint

Check method parameter spacing and return usage.