0
0
Rubyprogramming~10 mins

Block given? check 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 check if a block is given.

Ruby
def greet
  if [1]
    yield
  else
    puts "No block given"
  end
end
Drag options to blanks, or click blank then click option'
Ablock?
Bblock_given?
Cblock_given
Dgiven_block?
Attempts:
3 left
💡 Hint
Common Mistakes
Using block_given without the question mark
Using block? which is not a Ruby method
Using given_block? which does not exist
2fill in blank
medium

Complete the code to print a message only if a block is given.

Ruby
def check
  puts "Block is given" if [1]
end
Drag options to blanks, or click blank then click option'
Ablock_given?
Bblock_given
Cgiven_block?
Dblock?
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the question mark in block_given?
Using block_given without parentheses
3fill in blank
hard

Fix the error in the code to correctly check for a block.

Ruby
def run
  if [1]
    yield
  else
    puts "No block"
  end
end
Drag options to blanks, or click blank then click option'
Ablock_given?
Bblock_given
Cblock?
Dgiven_block?
Attempts:
3 left
💡 Hint
Common Mistakes
Using block_given without question mark
Using block? which is not a Ruby method
4fill in blank
hard

Fill both blanks to create a hash with word lengths only if block is given and length > 3.

Ruby
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Aword.length
Bblock_given?
Cword.length > 3
Dyield
Attempts:
3 left
💡 Hint
Common Mistakes
Using block_given? as a condition inside the hash comprehension
Using yield incorrectly in the hash comprehension
5fill in blank
hard

Fill all three blanks to create a hash with uppercase keys and values only if block is given and value > 0.

Ruby
result = { [1]: [2] for [3], v in data.items() if block_given? && v > 0 }
Drag options to blanks, or click blank then click option'
Ak.upcase
Bv
Ck
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using key instead of k as loop variable
Not converting keys to uppercase
Using block_given? incorrectly in the comprehension