We use dynamic and strong typing to help the computer understand and check the types of data while running programs. This helps catch mistakes and makes coding easier.
Dynamic typing vs strong typing in Ruby
# Ruby is dynamically typed and strongly typed x = 5 # x is an integer now x = "Hi" # x is now a string # Trying to add a number and a string causes an error 5 + "5" # TypeError
Dynamic typing means you don't have to say the type of a variable before using it.
Strong typing means the language checks types and won't let you mix incompatible types without explicit conversion.
a anytime because Ruby is dynamically typed.a = 10 puts a.class # Output: Integer a = "hello" puts a.class # Output: String
puts 5 + 3 # Output: 8 puts 5 + "3" # Raises TypeError
This program shows how Ruby lets you change the type of a variable easily (dynamic typing). But if you try to add a number and a string, Ruby will stop you with an error (strong typing).
x = 7 puts "x is a #{x.class} with value #{x}" x = "seven" puts "Now x is a #{x.class} with value #{x}" # This will cause an error: # puts 5 + "5"
Dynamic typing makes coding faster but you must be careful with data types.
Strong typing helps catch errors early by preventing wrong type operations.
In Ruby, you can convert types explicitly, for example: 5 + "5".to_i works fine.
Dynamic typing means variables can hold any type and change types anytime.
Strong typing means the language checks types and stops you from mixing incompatible types without conversion.
Ruby is both dynamically typed and strongly typed, giving flexibility and safety.