Arrays hold many items together. These methods help you count items, check if an item is there, or make nested arrays simple.
Array methods (length, include?, flatten) in Ruby
class Array # Returns the number of items in the array def length # built-in method end # Checks if the array includes a given item def include?(item) # built-in method end # Flattens nested arrays into one array def flatten # built-in method end end
These are built-in methods in Ruby's Array class.
Use include? with a question mark because it returns true or false.
numbers = [1, 2, 3, 4] numbers.length # => 4
fruits = ['apple', 'banana', 'cherry'] fruits.include?('banana') # => true fruits.include?('grape') # => false
nested = [1, [2, 3], [4, [5]]] nested.flatten # => [1, 2, 3, 4, 5]
empty = [] empty.length # => 0 empty.include?(1) # => false empty.flatten # => []
This program shows how to use length, include?, and flatten on arrays. It prints the original array, its length, checks for items, and flattens a nested array.
numbers = [10, 20, 30, 40] puts "Original array: #{numbers}" puts "Length: #{numbers.length}" puts "Includes 20? #{numbers.include?(20)}" puts "Includes 50? #{numbers.include?(50)}" nested_array = [1, [2, 3], [4, [5, 6]]] puts "\nNested array: #{nested_array}" flat_array = nested_array.flatten puts "Flattened array: #{flat_array}"
Time complexity: length is O(1), include? is O(n), flatten is O(n) where n is total elements.
Space complexity: flatten creates a new array, so O(n) extra space.
Common mistake: forgetting that include? checks for exact matches, so '20' (string) is different from 20 (number).
Use flatten when you want one simple list instead of nested lists. Use include? to check presence before searching or processing.
length tells how many items are in an array.
include? checks if an item is inside the array, returning true or false.
flatten turns nested arrays into one simple array.
Practice
length method do when called on an array in Ruby?Solution
Step 1: Understand the purpose of
Thelengthlengthmethod counts how many items are inside an array.Step 2: Compare with other methods
include?checks for presence,flattenmerges nested arrays, so they do different things.Final Answer:
Returns the number of elements in the array -> Option AQuick Check:
length= count items [OK]
- Confusing length with include? method
- Thinking length flattens nested arrays
- Assuming length removes duplicates
arr includes the number 5?Solution
Step 1: Recall the correct method name and syntax
The method to check presence isinclude?with a question mark at the end.Step 2: Verify method call on array
It should be called asarr.include?(5), notincludeorincludes?.Final Answer:
arr.include?(5) -> Option DQuick Check:
Correct method isinclude?with question mark [OK]
- Omitting the question mark in include?
- Using incorrect method names like includes? or has?
- Using parentheses incorrectly
arr = [1, [2, 3], [4, [5]]] puts arr.flatten.length
Solution
Step 1: Apply
The nested arrayflattento the array[1, [2, 3], [4, [5]]]becomes[1, 2, 3, 4, 5]after flattening.Step 2: Calculate the length of the flattened array
The flattened array has 5 elements, solengthreturns 5.Final Answer:
5 -> Option AQuick Check:
Flattened array length = 5 [OK]
- Counting nested arrays as single elements
- Forgetting flatten changes nested arrays
- Miscounting elements after flatten
nums contains the number 10, but it raises an error. What is the problem?
nums = [5, 10, 15] if nums.include(10) puts "Found 10" end
Solution
Step 1: Identify the method name error
The method to check presence isinclude?with a question mark, notinclude.Step 2: Understand why error occurs
Callingincludewithout question mark is undefined, causing an error.Final Answer:
The method should beinclude?with a question mark -> Option BQuick Check:
Useinclude?to check presence [OK]
- Using include without question mark
- Thinking flatten is needed to check presence
- Confusing number and string types
- Miswriting if statement syntax
data = [[1, 2], [3, [4, 5]], 6], which Ruby code correctly flattens it and checks if the number 4 is included?Solution
Step 1: Flatten the nested array first
Usedata.flattento convert nested arrays into a single-level array.Step 2: Check if 4 is included in the flattened array
Callinclude?(4)on the flattened array to get true or false.Final Answer:
data.flatten.include?(4) -> Option CQuick Check:
Flatten then include? is correct order [OK]
- Calling include? before flatten
- Using include without question mark
- Trying to flatten the number 4
- Incorrect method chaining order
Ready to prove it?
Pass the test and this pattern moves unknown
