Posts

Showing posts with the label Ruby

Why Your Ruby Scripts Keep Breaking Files (and How to Fix Them)

File Handling in Ruby — Reading, Writing, and What Could Go Wrong File Handling in Ruby — Reading, Writing, and What Could Go Wrong CodeCraft Diaries #9 • For Freshers, Students & Curious Developers “If you’ve never accidentally deleted a file while testing your script, are you even a developer?” Working with files is one of the first real-world things you’ll do in any programming language. Whether it's reading a config, logging errors, or saving user data — file I/O is everywhere. And Ruby? Ruby makes it refreshingly simple. Until you forget to close the file... 💀 📖 Reading from a File Let’s say we have a file called data.txt . File.open("data.txt", "r") do |file| puts file.read end What’s happening here? "r" means “read mode” file.read grabs the entire content The block auto-closes the file (thank you, Ruby!) ✍️ Writing to a File File.open("log....

Ruby Mixins: Reuse Code Like a Pro (Without Inheritance Headaches)

Image
Modules & Mixins in Ruby — Sharing is Caring (Without the Mess) Modules & Mixins in Ruby — Sharing is Caring (Without the Mess) CodeCraft Diaries #8 • For Ruby Freshers, Students & Developers "Code duplication is like wearing the same socks three days in a row — it gets smelly fast." Ever copy-pasted a method from one class to another and thought: “I’ll clean this later...” Spoiler: You won’t. 🙃 That’s where **Modules** and **Mixins** come in — Ruby’s way of letting you *share code like a pro*, without inheritance spaghetti or duplicate soup. 🤝 What Is a Module? Think of a module as a toolbox . It’s not a class. You can’t make objects out of it. But you can pack it with reusable methods and include it wherever needed. module Greetings def hello puts "Hello, Rubyist!" end end Now you’ve got a method... just chilling inside a module, waiting to be mixed in. 🧪 Using Modules...

Iterators in Ruby — Think Less Looping, More Logic

Image
Ruby Iterators — When Loops Just Aren’t Elegant Enough Ruby Iterators — When Loops Just Aren’t Elegant Enough Published: June 24, 2025 • CodeCraft Diaries #7 "If you've been writing `for` loops in Ruby like it's 1999, this one's for you." Let’s be real: loops are the bread and butter of programming. But in Ruby? You don’t just butter the bread — you toast it, drizzle it with honey, and serve it like a gourmet dev snack. Welcome to the elegant world of Ruby iterators — where looping is expressive, concise, and kinda beautiful. 🚶 Why Not Just Use a Loop? You can, of course: for i in 1..3 puts i end But Ruby gives us cooler tools. Imagine replacing that clunky loop with something like: (1..3).each { |i| puts i } Cleaner. Readable. Ruby-esque. 🔄 Meet Your Iterator Friends 1. each — The Friendly Tour Guide ["coffee", "code", "chai"].each do |item| puts ...

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

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

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

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