Favorite Ruby Methods: Part 1
On this episode of Ruby for All, Andrew and Julie discuss the cold temps where they live, their preferences for warmer temps, using layers to cope with the changing temperatures, and their experiences with snow sports. For the month of February, get ready because Andrew and Julie are doing something fun! They’ll be going through some of their favorite Ruby methods all month on every episode, focusing on a different type of object type. Today’s methods they discuss are map, flatten, uniq, shuffle, sample, count, any, and all. And, if that’s not enough, they also talk about Spaceship Operator and enumerator. If they missed your favorite array or they didn’t explain something well, let them know on Twitter. Download this episode now!
On this episode of Ruby for All, Andrew and Julie discuss the cold temps where they live, their preferences for warmer temps, using layers to cope with the changing temperatures, and their experiences with snow sports. For the month of February, get ready because Andrew and Julie are doing something fun! They’ll be going through some of their favorite Ruby methods all month on every episode, focusing on a different type of object type. Today’s methods they discuss are map, flatten, uniq, shuffle, sample, count, any, and all. And, if that’s not enough, they also talk about Spaceship Operator and enumerator. If they missed your favorite array or they didn’t explain something well, let them know on Twitter. Download this episode now!
[00:05:11] Andrew and Julie want to make February fun, so they tell us what they’re doing and how Julie came across this idea.
[00:06:32] Let’s start with Andrew’s favorite method, the array object, which is map.
[00:08:46] Array flatten is another one Andrew likes and he explains what it does, as well as flat map. Does flatten work on nested arrays?
[00:11:20] Julie tells us flatten passing in zero just returns the same way and explains what she noticed when using methods, and Andrew had no idea that you could pass level to flatten until now.
[00:12:15] The next method is uniq, which returns a new array, but only the ones that are not duplicated. Julie wonders if you can pass in a parameter or a block with this.
[00:15:54] Andrew doesn’t use this next method a lot which is shuffle, and this returns a new array with the elements of self-shuffled, self being the array this is called on. Did you know you can pass random to it?
[00:16:40] Julie brings up the next method which is sample on an array. Sample returns a random element from the array, and we hear she used shuffle and sample in practice.
[00:18:20] The last method is count, which returns the count of specified elements, and if you don’t pass it a block or an argument, it will return the count of all elements.
[00:20:55] When would you want to use a SQL count query versus not? Andrew talks about a great article you can check out on ActiveRecord, and to clarify, dot length is something you would NOT want to use with ActiveRecord, use size.
[00:22:02] Julie tells us she will use size and not worry if it’s loaded or not and Andrew reads something from an article Nate Berkopec wrote.
[00:22:46] The next method is min/max which returns a two-element array containing the minimum and the maximum value, and Andrew does his best to explain what Spaceship Operator and enumerator are.
[00:28:17] Julie touches on some other methods, these aliases for example, maps alias is collect, and filter select, inject, reject, detect, and inspect. There is one more array that Julie brings up which is any, all, none where you can pass the any method and a block, and it will return true if any one of the elements met that criteria.
Panelists:
Andrew Mason
Julie J.
Sponsors:
Links:
3 ActiveRecord Mistakes That Slow Down Rails Apps: Count, Where and Present-by Nate Berkopec of speedshop
Ruby’s Spaceship Operator-Tech Talent South
Ruby’s Spaceship Operator-Tech Talent South
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