0
0
Rubyprogramming~10 mins

Array methods (length, include?, flatten) in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array methods (length, include?, flatten)
Start with Array
Call length
Return number of elements
Call include?(item)
Check each element
If found, return true
If not found, return false
Call flatten
Check each element
If element is array, expand it
If not, keep element
Return new flattened array
End
Start with an array, then call length to get count, include? to check presence, or flatten to expand nested arrays into one.
Execution Sample
Ruby
arr = [1, [2, 3], 4]
len = arr.length
has_two = arr.include?(2)
flat = arr.flatten
This code gets the length of arr, checks if 2 is included, and flattens nested arrays.
Execution Table
StepActionArray StateVariableValueOutput
1Initialize arr[1, [2, 3], 4]arr[1, [2, 3], 4]
2Call arr.length[1, [2, 3], 4]len33
3Call arr.include?(2)[1, [2, 3], 4]has_twofalsefalse
4Call arr.flatten[1, [2, 3], 4]flat[1, 2, 3, 4][1, 2, 3, 4]
5End[1, [2, 3], 4]Execution complete
💡 All methods called and results stored; arr remains unchanged.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
arr[1, [2, 3], 4][1, [2, 3], 4][1, [2, 3], 4][1, [2, 3], 4][1, [2, 3], 4]
lenundefined3333
has_twoundefinedundefinedfalsefalsefalse
flatundefinedundefinedundefined[1, 2, 3, 4][1, 2, 3, 4]
Key Moments - 3 Insights
Why does arr.include?(2) return false even though 2 is inside a nested array?
include? checks only the top-level elements. Since 2 is inside [2, 3], which is a nested array, include? does not find 2 directly in arr (see step 3 in execution_table).
Does flatten change the original array arr?
No, flatten returns a new array with nested arrays expanded but does not modify arr itself (see variable_tracker for arr's unchanged state after step 4).
What does length count when the array has nested arrays?
length counts only the top-level elements, so nested arrays count as one element each (see step 2 in execution_table where length is 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the value of has_two after calling arr.include?(2)?
Afalse
Btrue
Cnil
Derror
💡 Hint
Check the 'Value' column at step 3 in execution_table.
At which step does the array get flattened into a single-level array?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the action 'Call arr.flatten' in execution_table.
If arr was [1, 2, 3], what would arr.include?(2) return?
Afalse
Btrue
Cnil
Derror
💡 Hint
include? returns true if the element is directly in the array; see variable_tracker for has_two.
Concept Snapshot
Array methods in Ruby:
- length: returns number of top-level elements
- include?(item): checks if item is a top-level element
- flatten: returns new array with nested arrays expanded
Original array stays unchanged by flatten.
Full Transcript
We start with an array arr containing numbers and a nested array. Calling length returns the count of top-level elements, which is 3. include?(2) checks if 2 is directly inside arr, but since 2 is inside a nested array, it returns false. flatten returns a new array where nested arrays are expanded into one level, resulting in [1, 2, 3, 4]. The original array arr remains unchanged throughout. These methods help us count, check presence, and simplify nested arrays easily.