Posts

Showing posts with the label DevDiaries

Error Handling in Ruby — Don’t Panic, Rescue!

Error Handling in Ruby — Don’t Panic, Rescue! 🚨 Error Handling in Ruby — Don’t Panic, Rescue! πŸš€ For students, freshers, and curious devs who want to survive Ruby's "uh-oh" moments. Let’s face it — coding without errors is like making Maggi without spilling a little masala. Impossible! 😜 So instead of throwing your laptop out of the window when Ruby screams at you, let’s learn to *rescue* our code like true heroes. Mini Story: Imagine you’re writing the next big Ruby app. Everything is going fine until suddenly… undefined method `sizee' for nil:NilClass (NoMethodError) That’s Ruby’s polite way of saying: “Yo, you messed up.” But instead of crying, we’ll put on our superhero cape and *rescue* the situation. 1. Meet begin and rescue — Your New Besties 🀝 Ruby gives us the begin … rescue block to handle errors like a pro. Think of it as Ruby saying: “Try this. ...

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 ...

This is Ruby. And Ruby knows how to block — like a bouncer for bad code. πŸ”₯

Image
Ruby Blocks — The Secret Sauce Behind Elegant Code Ruby Blocks — The Secret Sauce Behind Elegant Code Picture this: You're writing Ruby. Things are going well… until you find yourself copying the same chunk of logic again. And again. And… wait, again? 😩 Enter: Ruby Blocks — your new BFF for clean, DRY, and elegant code. If methods are the skeleton of Ruby, blocks are the juicy muscles that flex logic in powerful ways. 🍱 Blocks? Like, Tupperware Blocks? Exactly! Ruby blocks are like code Tupperware — you can pack them with logic, pass them around, and execute them on demand. They're anonymous chunks of code that you can hand over to a method to be called later. πŸ” Syntax: The Two Faces of Ruby Blocks 1. Curly Braces for the Quickies: [1, 2, 3].each { |num| puts num } 2. do...end for Multiline Zen: [1, 2, 3].each do |num| puts "Double trouble: #{num * 2}" end Both are blocks. One's espresso; the other's a cappuccino. ...