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

First element of the array

  • first
  • first?
names = ["Foo", "Bar"]
puts names.first # Foo
puts names[0]    # Foo

names = [] of String
# puts names.first
# Unhandled exception: Empty enumerable (Enumerable::EmptyError)

# puts names[0]
# Unhandled exception: Index out of bounds (IndexError)

first = names.first?
puts first.nil? # true
first = names[0]?
puts first.nil? # true