Bird
Raised Fist0
Rubyprogramming~20 mins

Rubocop for linting in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1.

What is the main purpose of using Rubocop in Ruby programming?

easy
A. To manage Ruby gem dependencies
B. To run Ruby programs faster
C. To check code style and find errors
D. To compile Ruby code into machine code

Solution

  1. Step 1: Understand Rubocop's role

    Rubocop is a tool that checks Ruby code for style issues and errors.
  2. Step 2: Compare options

    Only To check code style and find errors describes checking code style and errors, which matches Rubocop's purpose.
  3. Final Answer:

    To check code style and find errors -> Option C
  4. Quick Check:

    Rubocop = code style and error checker [OK]
Hint: Rubocop checks style and errors, not speed or compilation [OK]
Common Mistakes:
  • Thinking Rubocop runs or compiles code
  • Confusing Rubocop with dependency managers
  • Assuming Rubocop improves runtime speed
2.

Which command correctly runs Rubocop on your Ruby project from the terminal?

easy
A. execute rubocop
B. ruby rubocop
C. run rubocop
D. rubocop

Solution

  1. Step 1: Identify the correct command

    The standard command to run Rubocop is simply rubocop in the terminal.
  2. Step 2: Eliminate incorrect options

    Options B, C, and D are not valid terminal commands for Rubocop.
  3. Final Answer:

    rubocop -> Option D
  4. Quick Check:

    Run Rubocop with 'rubocop' command [OK]
Hint: Just type 'rubocop' in terminal to run it [OK]
Common Mistakes:
  • Adding 'ruby' before rubocop
  • Using 'run' or 'execute' commands incorrectly
  • Confusing Rubocop command with Ruby interpreter
3.

Given this Ruby code saved in example.rb:

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

greet("Alice")

You run rubocop example.rb and get a warning about missing a blank line after method definition. What will Rubocop suggest?

medium
A. Add a blank line after the end of the method
B. Remove the blank line before calling greet
C. Change puts to print
D. Rename the method to greeting

Solution

  1. Step 1: Understand Rubocop's style rule

    Rubocop often requires a blank line after method definitions for readability.
  2. Step 2: Identify the suggested fix

    The warning suggests adding a blank line after the end of greet method, before the next code.
  3. Final Answer:

    Add a blank line after the end of the method -> Option A
  4. Quick Check:

    Blank line after method end = Rubocop style [OK]
Hint: Rubocop likes blank lines after method ends [OK]
Common Mistakes:
  • Ignoring blank line warnings
  • Changing code logic instead of style
  • Misunderstanding the warning message
4.

Look at this Ruby code snippet:

def add(a, b)
  a + b
end

puts add(2, 3)

When running rubocop, you get an error about missing parentheses in method call. How do you fix it?

medium
A. Remove parentheses from def add(a, b)
B. Add parentheses around arguments in puts add(2, 3)
C. Add parentheses around a + b inside method
D. Change method name to add_numbers

Solution

  1. Step 1: Understand Rubocop's style on method calls

    Rubocop prefers explicit parentheses in method calls with arguments for clarity.
  2. Step 2: Apply fix to method call

    The call puts add(2, 3) should have parentheses around add(2, 3) to avoid ambiguity.
  3. Final Answer:

    Add parentheses around arguments in puts add(2, 3) -> Option B
  4. Quick Check:

    Method calls with args need parentheses [OK]
Hint: Put parentheses around method call arguments [OK]
Common Mistakes:
  • Removing parentheses from method definitions
  • Adding parentheses inside expressions unnecessarily
  • Ignoring Rubocop's parentheses style
5.

You want to configure Rubocop to ignore style warnings about line length in your project. Which file and setting should you modify?

hard
A. Add Metrics/LineLength: with Enabled: false in .rubocop.yml
B. Add LineLength: ignore in Gemfile
C. Set rubocop --ignore-line-length in terminal
D. Remove rubocop gem from Gemfile

Solution

  1. Step 1: Identify Rubocop config file

    Rubocop uses .rubocop.yml file to configure rules.
  2. Step 2: Disable line length check

    Inside .rubocop.yml, setting Metrics/LineLength: Enabled: false disables that check.
  3. Final Answer:

    Add Metrics/LineLength: with Enabled: false in .rubocop.yml -> Option A
  4. Quick Check:

    Disable rules in .rubocop.yml [OK]
Hint: Disable rules in .rubocop.yml config file [OK]
Common Mistakes:
  • Trying to disable rules via Gemfile
  • Using wrong command line flags
  • Removing Rubocop gem instead of configuring
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