Posts

Showing posts with the label Programming

Meet Ruby’s Secret Siblings: Procs & Lambdas Made Fun

Image
Procs and Lambdas in Ruby — The Cousins You Need to Know Procs and Lambdas in Ruby — The Cousins You Need to Know Published on June 09, 2025 • CodeCraft Diaries #6 "If methods are the parents and blocks are the kids, then Procs and Lambdas are the cool cousins who show up late but save the day with reusable logic." 🔍 Wait... Blocks Have Relatives? Yes, they do. Blocks are amazing, but sometimes you want to reuse them, store them in variables, or pass them around like a tray of samosas at a tech meetup. That’s where Procs and Lambdas come in — Ruby’s way of giving blocks a full-time job. 👶 Quick Recap: What’s a Block? def greet yield end greet { puts "Hello from the block!" } Blocks are anonymous chunks of code you can pass to methods. But blocks can’t live on their own — Procs and Lambdas can. 🧑‍🤝‍🧑 Meet Proc and Lambda 📦 What is a Proc? A Proc is like a block with a backpack — you can carry ...

Loops in Ruby — Teaching Your Code to Repeat

CodeCraft Diaries #4: Loops in Ruby — Teaching Your Code to Repeat CodeCraft Diaries #4: Loops in Ruby — Teaching Your Code to Repeat "Why did the Ruby developer go in circles? Because they couldn't break the loop!" Welcome back, CodeCrafters! 🎉 In our last adventure, we taught our code to make decisions using control flow. Now, it's time to teach it to repeat tasks efficiently. Let's dive into the world of loops in Ruby! 🔁 The Need for Loops Imagine you want to print "Hello, World!" five times. You could write: puts "Hello, World!" puts "Hello, World!" puts "Hello, World!" puts "Hello, World!" puts "Hello, World!" But that's not efficient. Instead, let's use a loop: 5.times do puts "Hello, World!" end Much better, right? 🌀 Types of Loops in Ruby 1. while Loop Repeats as long as a condition is true. i = 0 while i ...