Posts

Showing posts with the label Ruby on Rails

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

Because Copy-Pasting Is Not Coding - Ruby Loops Tutorial

Image
Loops in Ruby — Because Repeating Manually Is So 2000s | CodeCraft Diaries #4 Loops in Ruby — Because Repeating Manually Is So 2000s Imagine being told to print “I love Ruby” 100 times by hand. Sounds painful, right? Well, that’s what we used to do before loops existed (not really, but you get the point 😉). In today’s edition of CodeCraft Diaries , we’ll explore the magic of loops in Ruby — tools that make repetition effortless, elegant, and DRY (Don’t Repeat Yourself). Why Loops Matter Loops allow your code to execute a block repeatedly based on a condition or a set number of times. They're essential when you're handling lists, user input, data processing, and more. 1. while Loop The while loop runs as long as the condition is true . i = 0 while i Common mistake: Forgetting to increment the loop variable, which causes an infinite loop. 2. until Loop Think of until as the opposite of while — it runs until the cond...

Control Flow in Ruby — Teaching Your Code to Make Decisions

Image
Control Flow in Ruby — Teaching Your Code to Make Decisions 🧭 CodeCraft Diaries #3: Control Flow in Ruby — Teaching Your Code to Make Decisions “If coffee exists, drink it. Else, panic.” Congratulations, you just wrote your first decision in Ruby. Control flow is what gives your program a brain. It's how you get it to choose a path, evaluate a condition, and respond differently depending on what’s happening. 🌱 It Starts With a Question In real life, we make decisions constantly: If it’s raining, take an umbrella. If your code runs, you celebrate. Else, you debug 😭. In Ruby, we do the same using keywords like if , elsif , else , and unless . 🔄 If / Else in Action weather = "rainy" if weather == "sunny" puts "Wear sunglasses 😎" elsif weather == "rainy" puts "Take an umbrella ☔" else puts "Check the weather app 🤷" end Output: Take...

Variables and Data Types in Ruby — The Real Building Blocks

Image
  🚀 CodeCraft Diaries #2: Variables and Data Types in Ruby — The Real Building Blocks “Okay, I’ve installed Ruby... now what?” You stare at the terminal. Cursor blinking. Empty file. Welcome to the starting line, my friend. Let’s talk about the first real step in learning any programming language: variables and data types . It’s where code starts remembering stuff, making decisions, and acting like it knows things. 🧠 What’s a Variable? Let me paint a picture. You’ve got a backpack. You drop your snacks into one pocket, your charger in another, and your laptop in the big one. Each pocket = a variable. The stuff inside = the value. In Ruby, you just do this: snack = "Chips" drink = "Coffee" That’s it. No need to declare types or chant any mystical compiler spells. Ruby’s like: “I got you.” 🎨 The Basic Data Types (a.k.a The Stuff You Carry) 1. Strings – Just Text name = "Ruby" You can play with strings like this: puts ...

Why Every Developer Should Meet Ruby: A Friendly Introduction

Meet Ruby: The Language That Makes Coding Feel Human A beginner-friendly guide to why Ruby could be your new favorite language. 🧠 What Is Ruby? Ruby is a high-level, interpreted programming language that was designed with one clear goal: developer happiness . If you’ve ever felt like other programming languages are too rigid or robotic — Ruby changes that. It’s simple, expressive, and reads almost like plain English. Ruby lets you focus more on solving problems and less on battling syntax. 📜 A Quick Look Back: Ruby’s Origins Ruby was created in 1995 by Yukihiro “Matz” Matsumoto , a Japanese programmer who wanted a language that blended the best features of Perl, Smalltalk, and Lisp. His philosophy? “ Programming should be fun. ” And Ruby reflects that. It’s designed to be intuitive, elegant, and enjoyable to work with. 🎯 Why Learn Ruby in 2025? Even in a world full of Python, JavaScript, and Go — Ruby holds its own. Here’s why: Readable Syntax – You’ll write cod...