0
0
Rubyprogramming~10 mins

Block parameters 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 pass the block parameter correctly.

Ruby
5.times do |[1]|
  puts [1]
end
Drag options to blanks, or click blank then click option'
Aindex
Bitem
Cnum
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not inside the pipes.
Leaving the block parameter empty.
2fill in blank
medium

Complete the code to use block parameters to sum numbers.

Ruby
sum = 0
[1, 2, 3].each do |[1]|
  sum += [1]
end
puts sum
Drag options to blanks, or click blank then click option'
Avalue
Bnum
Celement
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name not matching the block parameter.
Trying to use the array name inside the block instead of the parameter.
3fill in blank
hard

Fix the error in the block parameter to print each key and value.

Ruby
{a: 1, b: 2}.each do |[1]|
  puts "#{ [1][0] }: #{ [1][1] }"
end
Drag options to blanks, or click blank then click option'
Akey
Bpair
Cvalue
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using separate parameters without parentheses.
Trying to access key and value directly without indexing.
4fill in blank
hard

Fill both blanks to correctly unpack key and value in the block parameters.

Ruby
{x: 10, y: 20}.each do |[1], [2]|
  puts "Key: #{ [1] }, Value: #{ [2] }"
end
Drag options to blanks, or click blank then click option'
Akey
Bvalue
Ck
Dv
Attempts:
3 left
💡 Hint
Common Mistakes
Using only one block parameter when two are needed.
Mixing up the order of key and value.
5fill in blank
hard

Fill all three blanks to create a hash with word lengths using block parameters and condition.

Ruby
words = ["apple", "bee", "cat"]
lengths = {}
words.each do |[1]|
  [2] = [1].[3]
  lengths[[1]] = [2] if [2] > 3
end
Drag options to blanks, or click blank then click option'
Aword
Blen
Clen(word)
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect variable names.
Not matching the condition with the value.