Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Type of array elements

values = [1, 2, "three", "four"]
puts typeof(values)
puts typeof(values[0])
puts typeof(values[2])
puts ""

values.each { |val|
  puts typeof(val)
  # puts val.size
  # Error: undefined method 'size' for Int32 (compile-time type is (Int32 | String))

  if val.is_a?(String)
    puts val.size
  end
}

All the elements of an array "inherit" the type from the array