0
0
Rubyprogramming~10 mins

Each for iteration 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 print each number in the array.

Ruby
numbers = [1, 2, 3]
numbers.[1] do |num|
  puts num
end
Drag options to blanks, or click blank then click option'
Areduce
Beach
Cmap
Dselect
Attempts:
3 left
💡 Hint
Common Mistakes
Using map instead of each when you only want to print values.
Using select which filters elements instead of iterating all.
2fill in blank
medium

Complete the code to print each word in uppercase.

Ruby
words = ['cat', 'dog', 'bird']
words.[1] do |word|
  puts word.upcase
end
Drag options to blanks, or click blank then click option'
Amap
Bselect
Creduce
Deach
Attempts:
3 left
💡 Hint
Common Mistakes
Using map which returns a new array instead of just printing.
Using select which filters elements.
3fill in blank
hard

Fix the error in the code to correctly iterate over the hash and print keys and values.

Ruby
person = {name: 'Alice', age: 30}
person.[1] do |key, value|
  puts "#{key}: #{value}"
end
Drag options to blanks, or click blank then click option'
Aselect
Bmap
Ceach
Dreduce
Attempts:
3 left
💡 Hint
Common Mistakes
Using map which returns a new array instead of just iterating.
Using select which filters elements.
4fill in blank
hard

Fill both blanks to create a hash with words as keys and their lengths as values.

Ruby
words = ['apple', 'banana', 'cherry']
lengths = words.map { |word| [[1], [2]] }.to_h
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Cword.length
Dword.size
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python's len() function instead of Ruby's length method.
Using word.size as key instead of value.
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 5 letters.

Ruby
words = ['apple', 'banana', 'cherry', 'date']
lengths = words.select { |word| word.length [3] 5 }.map { |word| [[1], [2]] }.to_h
Drag options to blanks, or click blank then click option'
Aword.upcase
Bword.length
C>
Dword.size
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python syntax like len(word) instead of Ruby methods.
Using < instead of > in the condition.