Bird
Raised Fist0
Rubyprogramming~10 mins

Array methods (length, include?, flatten) in Ruby - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the number of elements in the array.

Ruby
numbers = [1, 2, 3, 4, 5]
count = numbers.[1]
puts count
Drag options to blanks, or click blank then click option'
Atotal
Bsize
Ccount
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method that does not exist like 'total'.
Confusing 'length' with 'include?'.
2fill in blank
medium

Complete the code to check if the array contains the number 3.

Ruby
numbers = [1, 2, 3, 4, 5]
has_three = numbers.[1](3)
puts has_three
Drag options to blanks, or click blank then click option'
Ahas?
Binclude?
Ccontains?
Dexists?
Attempts:
3 left
💡 Hint
Common Mistakes
Using methods that do not exist like 'contains?'.
Forgetting the question mark at the end of 'include?'.
3fill in blank
hard

Fix the error in the code to flatten the nested array into a single array.

Ruby
nested = [[1, 2], [3, 4], [5]]
flat = nested.[1]
puts flat.inspect
Drag options to blanks, or click blank then click option'
Aflatten
Bflatten!
Cflat!
Dflat
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'flatten!' which modifies the original array instead of returning a new one.
Using non-existent methods like 'flat' or 'flat!'.
4fill in blank
hard

Fill both blanks to create a hash with words as keys and their lengths as values, only for words longer than 3 letters.

Ruby
words = ["apple", "cat", "banana", "dog"]
lengths = words.each_with_object({}) do |word, h|
  if word.[1] [2] 3
    h[word] = word.[1]
  end
end
puts lengths
Drag options to blanks, or click blank then click option'
A.length
B>
Clength
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '.length' which causes a syntax error (double dot).
Using '<' instead of '>' for the condition.
5fill in blank
hard

Fill all three blanks to create a hash with uppercase words as keys and their lengths as values, only for words longer than 3 letters.

Ruby
words = ["apple", "cat", "banana", "dog"]
lengths = words.each_with_object({}) do |word, h|
  if word.[2] [3] 3
    h[word[1]] = word.[2]
  end
end
puts lengths
Drag options to blanks, or click blank then click option'
A.upcase
Blength
C>
Dcapitalize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'capitalize' instead of 'upcase' for uppercase keys.
Using '<' instead of '>' in the condition.

Practice

(1/5)
1. What does the length method do when called on an array in Ruby?
easy
A. Returns the number of elements in the array
B. Checks if an element exists in the array
C. Flattens nested arrays into a single array
D. Removes duplicate elements from the array

Solution

  1. Step 1: Understand the purpose of length

    The length method counts how many items are inside an array.
  2. Step 2: Compare with other methods

    include? checks for presence, flatten merges nested arrays, so they do different things.
  3. Final Answer:

    Returns the number of elements in the array -> Option A
  4. Quick Check:

    length = count items [OK]
Hint: Remember length counts items, not checks or changes arrays [OK]
Common Mistakes:
  • Confusing length with include? method
  • Thinking length flattens nested arrays
  • Assuming length removes duplicates
2. Which of the following is the correct syntax to check if the array arr includes the number 5?
easy
A. arr.include(5)
B. arr.includes?(5)
C. arr.has?(5)
D. arr.include?(5)

Solution

  1. Step 1: Recall the correct method name and syntax

    The method to check presence is include? with a question mark at the end.
  2. Step 2: Verify method call on array

    It should be called as arr.include?(5), not include or includes?.
  3. Final Answer:

    arr.include?(5) -> Option D
  4. Quick Check:

    Correct method is include? with question mark [OK]
Hint: Remember Ruby methods with ? end with a question mark [OK]
Common Mistakes:
  • Omitting the question mark in include?
  • Using incorrect method names like includes? or has?
  • Using parentheses incorrectly
3. What is the output of the following Ruby code?
arr = [1, [2, 3], [4, [5]]]
puts arr.flatten.length
medium
A. 5
B. 3
C. 6
D. 7

Solution

  1. Step 1: Apply flatten to the array

    The nested array [1, [2, 3], [4, [5]]] becomes [1, 2, 3, 4, 5] after flattening.
  2. Step 2: Calculate the length of the flattened array

    The flattened array has 5 elements, so length returns 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Flattened array length = 5 [OK]
Hint: Flatten first, then count elements with length [OK]
Common Mistakes:
  • Counting nested arrays as single elements
  • Forgetting flatten changes nested arrays
  • Miscounting elements after flatten
4. The following code is intended to check if the array 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
medium
A. The array should be flattened before checking
B. The method should be include? with a question mark
C. The number 10 should be a string "10"
D. The if statement syntax is incorrect

Solution

  1. Step 1: Identify the method name error

    The method to check presence is include? with a question mark, not include.
  2. Step 2: Understand why error occurs

    Calling include without question mark is undefined, causing an error.
  3. Final Answer:

    The method should be include? with a question mark -> Option B
  4. Quick Check:

    Use include? to check presence [OK]
Hint: Check method names carefully; include? needs question mark [OK]
Common Mistakes:
  • Using include without question mark
  • Thinking flatten is needed to check presence
  • Confusing number and string types
  • Miswriting if statement syntax
5. Given the nested array data = [[1, 2], [3, [4, 5]], 6], which Ruby code correctly flattens it and checks if the number 4 is included?
hard
A. data.include?(4.flatten)
B. data.include?(4).flatten
C. data.flatten.include?(4)
D. data.flatten.include(4)

Solution

  1. Step 1: Flatten the nested array first

    Use data.flatten to convert nested arrays into a single-level array.
  2. Step 2: Check if 4 is included in the flattened array

    Call include?(4) on the flattened array to get true or false.
  3. Final Answer:

    data.flatten.include?(4) -> Option C
  4. Quick Check:

    Flatten then include? is correct order [OK]
Hint: Flatten first, then check inclusion with include? [OK]
Common Mistakes:
  • Calling include? before flatten
  • Using include without question mark
  • Trying to flatten the number 4
  • Incorrect method chaining order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard