0
0
Rubyprogramming~20 mins

Ruby style guide essentials - Practice Problems & Coding Challenges

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