Block given? check
📖 Scenario: You are creating a simple Ruby method that behaves differently depending on whether a block is given or not. This is useful when you want to allow extra customization but also have a default behavior.
🎯 Goal: Build a Ruby method called greet that prints a default greeting if no block is given, and uses the block to customize the greeting if a block is provided.
📋 What You'll Learn
Create a method named
greetUse
block_given? inside the method to check if a block is passedPrint
"Hello, World!" if no block is givenIf a block is given, call the block and print its result
💡 Why This Matters
🌍 Real World
Checking if a block is given allows Ruby methods to be flexible and customizable, which is common in libraries and frameworks.
💼 Career
Understanding blocks and <code>block_given?</code> is essential for Ruby developers, especially when working with Rails or writing reusable code.
Progress0 / 4 steps