Understanding Dynamic Typing vs Strong Typing in Ruby
📖 Scenario: Imagine you are building a simple calculator program that can add numbers or concatenate words. Ruby lets you use the same variable for different types of data because it uses dynamic typing. But it also checks types strictly when you try to do operations that don't make sense, showing strong typing.
🎯 Goal: You will create variables with different types, try to add them, and see how Ruby handles dynamic and strong typing.
📋 What You'll Learn
Create variables with different types (integer and string)
Create a variable to hold a sum or concatenation
Try to add variables of the same type
Try to add variables of different types and observe the error
💡 Why This Matters
🌍 Real World
Understanding how Ruby handles types helps avoid bugs when working with user input, calculations, or text processing.
💼 Career
Many Ruby jobs require clear understanding of dynamic and strong typing to write reliable and error-free code.
Progress0 / 4 steps
1
Create variables with different types
Create a variable called number and set it to the integer 10. Then create a variable called text and set it to the string "10".
Ruby
Hint
Use = to assign values. Strings need quotes.
2
Create a variable to hold the result
Create a variable called result and set it to 0 as a starting point.
Ruby
Hint
Initialize result with 0 to prepare for addition.
3
Add variables of the same type
Set result to the sum of number plus 20. Then set result to the concatenation of text plus "20".
Ruby
Hint
Use + to add numbers or join strings.
4
Try to add variables of different types and observe error
Try to set result to number plus text. Then print result. This will cause an error because Ruby is strongly typed and does not allow adding integer and string directly.
Ruby
Hint
Ruby will raise a TypeError because it cannot add integer and string directly.
Practice
(1/5)
1. Which statement best describes Ruby's typing system?
easy
A. Ruby is dynamically typed and strongly typed.
B. Ruby is statically typed and weakly typed.
C. Ruby is statically typed and strongly typed.
D. Ruby is dynamically typed and weakly typed.
Solution
Step 1: Understand dynamic typing
Dynamic typing means variables can hold any type and change types anytime during execution.
Step 2: Understand strong typing
Strong typing means the language prevents mixing incompatible types without explicit conversion.
Final Answer:
Ruby is dynamically typed and strongly typed. -> Option A
Quick Check:
Dynamic + Strong = C [OK]
Hint: Remember Ruby allows type changes but checks type safety [OK]
Common Mistakes:
Confusing dynamic with static typing
Thinking Ruby allows unsafe type mixing
Mixing weak and strong typing concepts
2. Which Ruby code snippet shows dynamic typing in action?
easy
A. x = 10; x = "hello"
B. int x = 10; x = "hello"
C. var x: Integer = 10; x = "hello"
D. let x = 10; x = true
Solution
Step 1: Identify Ruby syntax
Ruby allows assigning different types to the same variable without error.
Step 2: Check each option
x = 10; x = "hello" uses Ruby syntax and changes x from integer to string, showing dynamic typing.
Final Answer:
x = 10; x = "hello" -> Option A
Quick Check:
Variable changes type dynamically = A [OK]
Hint: Look for variable reassignment with different types in Ruby syntax [OK]
Common Mistakes:
Choosing code with static typing syntax
Confusing variable declaration styles
Ignoring language-specific syntax rules
3. What will be the output of this Ruby code?
def add(a, b)
a + b
end
puts add(5, "10")
medium
A. 15
B. TypeError
C. 510
D. nil
Solution
Step 1: Analyze the method call
The method add tries to add an integer (5) and a string ("10") using + operator.
Step 2: Understand Ruby's strong typing
Ruby does not automatically convert types for +; adding integer and string causes a TypeError.
Final Answer:
TypeError -> Option B
Quick Check:
Strong typing prevents int + string = B [OK]
Hint: Adding different types without conversion causes TypeError [OK]
Common Mistakes:
Assuming automatic string conversion
Expecting concatenation of numbers and strings
Ignoring Ruby's type safety rules
4. Identify the error in this Ruby code:
def multiply(x, y)
x * y
end
result = multiply(3, "4")
puts result
medium
A. NameError for undefined variable
B. SyntaxError due to method definition
C. No error, outputs 12
D. TypeError due to multiplying integer and string
Solution
Step 1: Check method parameters and call
The method multiply is called with an integer (3) and a string ("4").
Step 2: Understand Ruby's type rules for *
Multiplying integer by string without conversion causes a TypeError in Ruby.
Final Answer:
TypeError due to multiplying integer and string -> Option D
Quick Check:
Strong typing blocks int * string = A [OK]
Hint: Check operand types for * operator to avoid TypeError [OK]
Common Mistakes:
Thinking Ruby auto-converts string to integer
Assuming no error on mixed types
Confusing SyntaxError with TypeError
5. You want to write a Ruby method that adds two values but safely handles if one is a string representing a number. Which approach best uses Ruby's dynamic and strong typing?
hard
A. Convert both to strings and concatenate.
B. Add values directly without conversion.
C. Convert both to integers before adding using to_i method.
D. Use static type declarations for parameters.
Solution
Step 1: Understand dynamic typing flexibility
Ruby variables can hold any type, so inputs can be string or integer.
Step 2: Use strong typing safety
To avoid TypeError, convert strings to integers explicitly before adding.
Step 3: Evaluate options
Converting both to integers using to_i before adding is safe. Adding directly risks TypeError. Static type declarations are invalid in Ruby. Converting to strings performs concatenation instead of addition.
Final Answer:
Convert both to integers before adding using to_i method. -> Option C