0
0
Rubyprogramming~10 mins

Map/collect for transformation in Ruby - Interactive Code Practice

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

Complete the code to transform each number by doubling it using map.

Ruby
numbers = [1, 2, 3, 4]
doubled = numbers.[1] { |n| n * 2 }
puts doubled.inspect
Drag options to blanks, or click blank then click option'
Aselect
Bmap
Ceach
Dcollect
Attempts:
3 left
💡 Hint
Common Mistakes
Using each which returns the original array without changes.
Using select which filters elements instead of transforming.
2fill in blank
medium

Complete the code to convert all strings in the array to uppercase using collect.

Ruby
words = ['apple', 'banana', 'cherry']
upcased = words.[1] { |word| word.upcase }
puts upcased.inspect
Drag options to blanks, or click blank then click option'
Aeach
Bselect
Ccollect
Dreject
Attempts:
3 left
💡 Hint
Common Mistakes
Using each which does not return a transformed array.
Using select which filters elements.
3fill in blank
hard

Fix the error in the code to correctly transform numbers by adding 5 using map.

Ruby
nums = [10, 20, 30]
result = nums.[1] { |num| num + 5 }
puts result.inspect
Drag options to blanks, or click blank then click option'
Amap
Bselect
Ceach
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using each which does not transform the array.
Using select which filters elements.
4fill in blank
hard

Fill both blanks to create a hash where keys are words and values are their lengths using map.

Ruby
words = ['dog', 'cat', 'bird']
lengths = words.[1] { |word| [word, word.[2]] }.to_h
puts lengths.inspect
Drag options to blanks, or click blank then click option'
Amap
Beach
Clength
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using each which does not return a transformed array.
Using size which also works but is less common than length.
5fill in blank
hard

Fill all three blanks to transform an array of numbers into a hash with keys as strings and values as squares.

Ruby
nums = [1, 2, 3]
squares = nums.[1] { |n| [n.[2].to_s, n [3] 2] }.to_h
puts squares.inspect
Drag options to blanks, or click blank then click option'
Amap
Bto_s
C**
Deach
Attempts:
3 left
💡 Hint
Common Mistakes
Using each which does not transform the array.
Using * instead of ** for squaring.