0
0
Rubyprogramming~10 mins

Dynamic typing vs strong typing in Ruby - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Dynamic typing vs strong typing
Start
Assign variable
Dynamic typing: variable type decided at runtime
Use variable in operation
Strong typing: type rules enforced
If type mismatch: Error
Operation succeeds if types compatible
End
This flow shows how Ruby assigns variable types at runtime (dynamic typing) and enforces type rules during operations (strong typing).
Execution Sample
Ruby
x = 5
x = "hello"
result = x + 10
Assigns an integer to x, then a string, then tries to add a number to a string causing a type error.
Execution Table
StepCode LineVariable StateOperationResult/Output
1x = 5x = 5 (Integer)Assign integer 5 to xx holds integer 5
2x = "hello"x = "hello" (String)Assign string "hello" to xx now holds string "hello"
3result = x + 10x = "hello" (String)Add integer 10 to string xTypeError: String can't be coerced into Integer
💡 Execution stops at step 3 due to type error from adding integer to string.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
xundefined5 (Integer)"hello" (String)"hello" (String)
resultundefinedundefinedundefinedundefined (error, not assigned)
Key Moments - 2 Insights
Why can x change from an integer to a string without error?
Because Ruby uses dynamic typing, the variable x can hold any type and changes type at runtime as shown in steps 1 and 2.
Why does adding a number to a string cause an error?
Ruby is strongly typed, so it does not allow mixing incompatible types in operations, causing a TypeError at step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the type of x after step 2?
AInteger
BString
CFloat
DUndefined
💡 Hint
Check the 'Variable State' column at step 2 in the execution table.
At which step does the program stop due to a type error?
AStep 1
BStep 2
CStep 3
DNo error occurs
💡 Hint
Look at the 'Result/Output' column for the error message.
If Ruby was not strongly typed, what would happen at step 3?
AIt would perform the addition without error
BIt would convert string to integer automatically
CIt would raise a different error
DIt would convert integer to string automatically
💡 Hint
Strong typing enforces type rules; without it, operations might succeed despite type differences.
Concept Snapshot
Dynamic typing means variables can hold any type and change type at runtime.
Strong typing means operations enforce type rules and prevent mixing incompatible types.
Ruby is dynamically and strongly typed.
Example: x = 5; x = "hello" works, but x + 10 causes error.
This helps catch type errors early during execution.
Full Transcript
This visual execution shows how Ruby handles dynamic and strong typing. First, variable x is assigned the integer 5. Then x is reassigned to the string "hello" without error because Ruby uses dynamic typing, allowing variables to change types at runtime. Next, the code tries to add 10 (an integer) to x (a string). Ruby raises a TypeError because it is strongly typed and does not allow mixing incompatible types in operations. The execution table tracks each step, showing variable states and the error at step 3. This helps learners see how dynamic typing allows flexible variable types, while strong typing enforces safe operations.