Bird
Raised Fist0
Rubyprogramming~20 mins

Ruby style guide essentials - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Style Guide 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 this Ruby code snippet?

Consider the following Ruby code. What will it print?

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

greet("alice")
AHello, Alice!
BHello, alice!
Chello, Alice!
DHELLO, ALICE!
Attempts:
2 left
💡 Hint

Look at the capitalize method and how it changes the string.

🧠 Conceptual
intermediate
1:30remaining
Which is the preferred Ruby style for method names?

According to Ruby style guide essentials, which method name style is preferred?

APascalCaseMethod
Bsnake_case_method
CcamelCaseMethod
Dkebab-case-method
Attempts:
2 left
💡 Hint

Think about how Ruby methods are usually named to improve readability.

🔧 Debug
advanced
2:00remaining
What error does this Ruby code raise?

Examine the code below. What error will it raise when run?

Ruby
numbers = [1, 2, 3]
puts numbers[3].to_s
ANilClass error
BIndexError
CNo error, prints empty string
DNoMethodError
Attempts:
2 left
💡 Hint

What does Ruby return when accessing an array index that is out of range?

📝 Syntax
advanced
2:30remaining
Which option correctly defines a Ruby class with an initialize method?

Choose the correct Ruby syntax for defining a class Person with an initialize method that takes a name parameter.

A
class Person
  def initialize(name)
    @name = name
  end
end
B
class Person
  def initialize name
    name = @name
  end
end
C
class Person
  def initialize(name):
    @name = name
  end
end
D
class Person
  def initialize(name)
    name = @name
end
Attempts:
2 left
💡 Hint

Remember Ruby uses def and end to define methods and classes, and instance variables start with @.

🚀 Application
expert
2:00remaining
How many key-value pairs are in the resulting hash?

What is the number of key-value pairs in the hash created by this Ruby code?

Ruby
hash = { a: 1, b: 2, a: 3, c: 4 }
A4
B1
C2
D3
Attempts:
2 left
💡 Hint

Consider what happens when a hash has duplicate keys.

Practice

(1/5)
1. According to the Ruby style guide essentials, which naming convention should you use for method names?
easy
A. snake_case
B. CamelCase
C. kebab-case
D. PascalCase

Solution

  1. Step 1: Understand method naming conventions

    Ruby style guide recommends using snake_case for method names to improve readability.
  2. Step 2: Compare options with style guide

    CamelCase and PascalCase are used for classes and modules, not methods.
  3. Final Answer:

    snake_case -> Option A
  4. Quick Check:

    Method names use snake_case [OK]
Hint: Methods and variables use snake_case, classes use CamelCase [OK]
Common Mistakes:
  • Using CamelCase for method names
  • Using kebab-case which is invalid in Ruby
  • Confusing class naming with method naming
2. Which of the following is the correct way to indent Ruby code according to the style guide?
easy
A. 2 spaces
B. 4 spaces
C. 1 tab
D. No indentation

Solution

  1. Step 1: Recall indentation rules

    The Ruby style guide specifies using 2 spaces for indentation to keep code clean and consistent.
  2. Step 2: Evaluate options

    4 spaces and tabs are common in other languages but not recommended in Ruby style guide.
  3. Final Answer:

    2 spaces -> Option A
  4. Quick Check:

    Indent with 2 spaces [OK]
Hint: Always indent Ruby code with 2 spaces, never tabs [OK]
Common Mistakes:
  • Using tabs instead of spaces
  • Using 4 spaces like in Python or JavaScript
  • No indentation causing unreadable code
3. What will be the output of the following Ruby code?
class MyClass
  def greet
    name = 'Alice'
    puts "Hello, #{name}!"
  end
end

obj = MyClass.new
obj.greet
medium
A. SyntaxError
B. Hello, #{name}!
C. Hello, name!
D. Hello, Alice!

Solution

  1. Step 1: Understand string interpolation

    Double quotes allow #{name} to be replaced by the variable's value, which is 'Alice'.
  2. Step 2: Trace method call and output

    Calling greet prints "Hello, Alice!" to the console.
  3. Final Answer:

    Hello, Alice! -> Option D
  4. Quick Check:

    Double quotes enable interpolation = Hello, Alice! [OK]
Hint: Double quotes allow #{var} interpolation, single quotes do not [OK]
Common Mistakes:
  • Using single quotes expecting interpolation
  • Confusing variable name with string literal
  • Expecting syntax error from interpolation
4. Identify the style error in the following Ruby code snippet:
class my_class
  def say_hello
    puts 'Hello world'
  end
end
medium
A. Class name should be snake_case
B. Method name should be CamelCase
C. Class name should be CamelCase
D. String should use double quotes

Solution

  1. Step 1: Check class naming conventions

    Ruby style guide requires class names to use CamelCase, not snake_case.
  2. Step 2: Verify method and string style

    Method name is correctly snake_case; single quotes are allowed if no interpolation needed.
  3. Final Answer:

    Class name should be CamelCase -> Option C
  4. Quick Check:

    Classes use CamelCase [OK]
Hint: Classes and modules use CamelCase, not snake_case [OK]
Common Mistakes:
  • Using snake_case for class names
  • Changing method names to CamelCase incorrectly
  • Forcing double quotes when not needed
5. You want to define a Ruby method that returns a greeting with a name variable. Which style is best according to the Ruby style guide essentials?
def greetUser(name)
  return 'Hello, ' + name + '!'
end
hard
A. def greetUser(name) "Hello, #{name}!" end
B. def greet_user(name) "Hello, #{name}!" end
C. def greet_user(name) 'Hello, ' + name + '!' end
D. def greetUser(name) 'Hello, ' + name + '!' end

Solution

  1. Step 1: Apply method naming conventions

    Method names should use snake_case, so greet_user is correct.
  2. Step 2: Use string interpolation with double quotes

    Double quotes allow cleaner string interpolation instead of concatenation.
  3. Final Answer:

    def greet_user(name) "Hello, #{name}!" end -> Option B
  4. Quick Check:

    snake_case + interpolation = def greet_user(name) "Hello, #{name}!" end [OK]
Hint: Use snake_case for methods and double quotes for interpolation [OK]
Common Mistakes:
  • Using CamelCase for method names
  • Using single quotes with interpolation expecting it to work
  • Concatenating strings instead of interpolation
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