0
0
Rubyprogramming~20 mins

Why dynamic typing matters in Ruby - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Dynamic Typing 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 with dynamic typing?

Look at this Ruby code. What will it print?

Ruby
x = 10
x = "hello"
puts x
A10
BTypeError
Chello
Dnil
Attempts:
2 left
💡 Hint

Remember, Ruby lets you change the type of a variable anytime.

🧠 Conceptual
intermediate
2:00remaining
Why does Ruby allow changing variable types?

Why is it useful that Ruby lets you assign different types to the same variable?

AIt makes code shorter and easier to write without declaring types.
BIt forces the programmer to declare types before use.
CIt prevents any errors from happening at runtime.
DIt makes the program run faster by skipping type checks.
Attempts:
2 left
💡 Hint

Think about how much typing you save when you don't have to say the type.

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

What error will this Ruby code produce?

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

puts add(5, "3")
ASyntaxError: unexpected string literal
BNoMethodError: undefined method '+' for Integer
CArgumentError: wrong number of arguments
DTypeError: String can't be coerced into Integer
Attempts:
2 left
💡 Hint

Think about what happens when you add a number and a string in Ruby.

📝 Syntax
advanced
2:00remaining
Which option correctly defines a method that accepts any type in Ruby?

Choose the correct Ruby method definition that accepts any type of argument and returns it.

A
def echo(x: String)
  return x
end
B
def echo(x)
  return x
end
C
def echo(x Integer)
  return x
end
D
def echo(x: Integer)
  return x
end
Attempts:
2 left
💡 Hint

Remember, Ruby does not require type declarations in method parameters.

🚀 Application
expert
3:00remaining
What is the output of this Ruby code demonstrating dynamic typing?

What will this Ruby code print?

Ruby
items = [1, "two", 3.0, :four]
items.each do |item|
  case item
  when Integer then puts "Integer: #{item * 2}"
  when String then puts "String: #{item.upcase}"
  when Float then puts "Float: #{item / 2}"
  else puts "Other: #{item}" end
end
A
Integer: 2
String: TWO
Float: 1.5
Other: :four
B
Integer: 2
String: two
Float: 1.5
Other: four
C
Integer: 1
String: TWO
Float: 3.0
Other: four
D
Integer: 2
String: TWO
Float: 1.5
Other: four
Attempts:
2 left
💡 Hint

Look carefully at how Ruby treats symbols and string methods.