Favorite Ruby Methods: Part 2 - The Hash
On this episode of Ruby for All, Julie tells us she’s working on a new app and she’s calling it “Today,” which is going to help her organize her day-to-day tasks. We’re also continuing with our February series talking about different object types in Ruby and our favorite methods with them. Today, we’re talking about the Ruby Hashes, such as merge, fetch, dig, .new, values, transform values, reject, key?, and compact. Hash is so important for Ruby developers, because the more you know, the better you can write. Julie tells us how she couldn’t grasp the Hash object when she first was learning Ruby, and Andrew shares his journey of learning other programming languages before learning Ruby and how it helped him appreciate Ruby. After today, he’s happy to know when to use dig now! Julie and Andrew hope this episode helps you like it’s helped them! Download this episode now find out more!
[00:01:13] Since Julie’s been building a new app for herself to stay organized daily, she tells us about playing around with ChatGPT, and Andrew explains ChatGPT is trained better on source code and other things.
[00:03:10] Andrew talks about GitHub’s Copilot and what it does.
[00:03:55] Our February series is continuing with talking about different object types in Ruby and we’re starting with a Hash, which is a dictionary light collection of unique keys and their values. Also called Associative Arrays, they’re similar to Array, but where an array uses integers its index, a hash allows you to use an object type. Julie wonders if the key can be any object.
[00:10:29] Andrew goes into Hash merge, which returns a new Hash formed by merging each of the other hashes into a copy of self, and he explains why you would merge.
[00:12:26] Andrew uses this next one constantly, Hash fetch, and this one returns the value for a given key if found. Julie seems to like this one, and if you’re building a lot of components, you should check this out.
[00:15:13] Julie brings up Hash dig, which extracts the nested values. We also learn Andrew doesn’t use Dig a lot because he can never remember when he’s supposed to use it!
[00:16:45] The next one is Hash.new, which returns a new empty hash object. Julie and Andrew tell us more about this one. If anyone has a use case for Hash.new and passing in, please let us know.
[00:20:20] Hash values at is the next one, and this returns a new array containing the values for the given keys. Andrew thinks he could use this more than he does.
[00:22:34] Julie explains Hash transform values, which returns a new hash object, and each entry has a key from self, a value provided by the block. How can you use this?
[00:24:28] Next one is Hash reject, which returns a new hash object whose entries are all of those from self for which the block returns false or nil. Julie asks Andrew if we’re rejecting the keys, values, or either.
[00:26:14] We made it to the last two for today! Andrew and Julie discuss Hash key? and Hash compact. Julie likes compact because it’s nice to be able to remove anything with no values.
Panelists:
Andrew Mason
Julie J.
Sponsors:
Links:
Creators and Guests
Host
Andrew Mason
Senior Product Developer at Podia, co-host of the Remote Ruby podcast, and co-editor of the Ruby Radar newsletter