As I’ve missed my last 2 posts, I’m going to now regale you with not 1, not 2, but 3 (three!) of my favorite little hacks ruby hacks.
Do you have a deeply nested structures you want 1 value from deep inside? Don’t care if the path is broken?
Use digg
! I especially love this to get deeply nested data out of a JSON response.
(Would you believe there is no digging related emoji? Have a 📯 instead.)
Sometimes we switch on classes, you’ve seen this before right?
Not only is that ugly, but we are basing our behavior on classes, instead of behavior. Wouldn’t it be cool if we could test for methods? Well we can!
Ugh… That looks horrible 😷. Let’s improve that with:
Adding a simple unary method on symbol gives us a great looking switch. ✨
If I could love code, this would be the code I love. 💞💗💖
Often times it makes sense for the to_s
value of an object to be the name or the title of the person or thing.
So out of shear laziness 😫 I wrote this little hack.
It might be a hack, but I like it.
Do you have your favorite hacks? What do you think of mine? Let me know below!