Posts

Showing posts with the label SelfTaughtDev

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

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

Unleashing Ruby Methods: Write Less, Do More 🔁

Image
🚀 CodeCraft Diaries #4: Methods in Ruby — DRY Up That Code! 🚀 CodeCraft Diaries #4: Methods in Ruby — DRY Up That Code! "Copy-paste is not a strategy. Reuse is." So far in our Ruby journey, you've seen variables, data types, and how to make decisions with control flow. Now it's time to explore the most powerful tool in your dev toolkit: 🛠 Methods — Your Code’s Superpower Think of a method as your personal code assistant. Instead of repeating the same logic over and over, you wrap it in a neat little function (method) and call it whenever you need it. 📦 Defining a Method Here’s how you define a method in Ruby: def greet puts "Hello there!" end Now just call it: greet # Output: Hello there! Simple, right? But wait, there’s more… 🎯 Methods with Parameters Want to greet someone by name? def greet(name) puts "Hello, #{name}!" end greet("Ruby") # ...