Member-only story
💎 Ruby Hacks You’ve Never Ever Used (But Should!) 🚀
Ruby is already a beautiful language — simple, elegant, and developer-friendly. But beneath its simplicity, there are hidden hacks and tricks that even seasoned developers rarely touch. 🤫
In this blog, we’ll uncover Ruby hacks you’ve probably never used before — with clear use cases, code examples, and pro tips. Let’s make your Ruby coding smarter and cooler! 😎✨
1️⃣
tap– Debug & Chain Like a Pro 🔍
The tap method lets you "tap into" an object, run operations, and still return the object itself.
Example: Debugging in the middle of a chain
user = { name: "Raj", age: 27 }
.tap { |u| puts "Before update: #{u}" }
.merge(age: 28)
.tap { |u| puts "After update: #{u}" }👉 Output:
Before update: {:name=>"Raj", :age=>27}
After update: {:name=>"Raj", :age=>28}💡 Tip: Use tap to peek into objects while chaining methods without breaking the flow. Perfect for debugging or logging!
2️⃣
then– Functional Style ✨
Introduced in Ruby 2.6, then works like tap but returns the block result instead of the object.