Search⌘ K

Closer Look into Arrays

Explore fundamental array operations in Ruby including initialization, querying, and updating. Learn key methods such as empty?, count, and include? alongside Rails-specific helpers like blank? and present?. This lesson helps you understand how to work efficiently with arrays and write cleaner Ruby code.

Simplifying arrays

The array is an essential data structure. Every programmer should know how to effectively query and update arrays. Ruby offers various methods to simplify array operations, like lookups, updates, calculating several matches based on criteria, adding, removing, bulk operations over all elements, and so on. Ruby’s standard library is compelling, predictable, and straightforward. Programmers love to use it. We hope that you’ll enjoy manipulating arrays with Ruby. Let’s get started!

The empty? method

The question mark indicates that the method returns a Boolean type, true or false, at the end of a method. We will use the empty? method to check whether the array has elements or if it is empty. If the array is empty, the method will return true.

$ pry
> [].empty?
 => true

Remember that the nil ...