Given the following Ruby code, what output will Rubocop produce regarding style offenses?
def greet(name) puts "Hello, #{name}" end greet("Alice")
Look for string style offenses in the code.
Rubocop prefers single quotes for strings without interpolation. The string "Hello, #{name}" uses double quotes but includes interpolation, so it is correct. However, the argument to greet is a double-quoted string without interpolation, which triggers the Style/StringLiterals offense.
Which Rubocop cop is responsible for detecting variables that are assigned but never used in Ruby code?
Think about the cop that warns about useless assignments.
The Lint/UselessAssignment cop detects variables that are assigned a value but never used later in the code.
Consider this Ruby code snippet. Rubocop raises an error. What is the cause?
class Person def initialize(name) @name = name end def greet puts "Hello, #{name}" end end
Check variable scope inside methods.
The instance variable @name is assigned in initialize, but the greet method tries to use name without the @ sign, which is undefined in that scope. Rubocop detects this as a NameError risk.
Which of the following Ruby code snippets will cause Rubocop to report a syntax offense?
Look for indentation and end keyword placement.
Rubocop enforces consistent indentation. Option A has the end keyword indented incorrectly, causing a style offense.
Analyze the following Ruby code. How many style offenses will Rubocop report?
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
Check method parameter spacing and return usage.
Rubocop will report two offenses: one for missing space after comma in def add(a,b) and one for unnecessary return in divide method.