When I first started out, there was a senior engineer that I worked with who was a wizard when it came to working with arrays. He taught me all the...
For further actions, you may consider blocking this person and/or reporting abuse
Awesome article! I use the
with_index
method quite often. Another option iseach_with_index
.At first, there appears to be no difference until you take into account that
with_index
accepts an optional "start index". Therefore you can start your array index at 1 by doingWHAT?!!!!!! I had no clue, thank you for sharing!!!
Fantastic article Molly! I have been using
for ages without knowing there was a better alternative!
Guess it pays to keep a beginner's mind. I've been using Ruby professionally for 7+ years and some of these were new to me.
They keep adding new toys to the language. One of my more recent discoveries is
transform_keys
andtransform_values
forHash
which they shipped without fanfare in Ruby 2.4. That's proven really valuable for "symbolizing" keys and cleaning up values.Nice! As a beginner in Ruby, this is really helpful.
The thing that always trips me up is forgetting the
|
to surround the current element and using(
instead of{
I keep thinking like ES6 in Ruby 😳
Additional Bonus for you
flatten
. Flatten, as name suggests flatten out an array no matter how much nesting level is involved for an array. Below is the example for 3 level nestingAlso, you can do
a.flatten!
, which will overwritea
itself and assign the new value.Thanks for the awesome article Molly! I found it very helpful, the examples are clear and concise!
I've created a short cheatsheet on a Paper doc so people can keep it handy: paper.dropbox.com/doc/Ruby-Array-M...
Wow, this is really fantastic! Thank you for putting that together. I will keep this in mind for future posts.
Do you mind if I add a link to your cheatsheet at the bottom of the post and share it on Twitter? I think people will find it very useful and I will definitely be sure to give you credit for it.
Of course! After all, it is your work!!!
I've used some of these methods before but it was great to learn new ones such as detect and reject! I also never realized that some of these methods return the original array. Thank you for pointing that out and thank you for such a great tutorial!
What I LOVE about this tutorial, Molly, is how you include examples of how you used to code, before becoming proficient at these different methods. I laughed at each "newbie example" you gave, cos that's exactly how I've been doing things too! LOL. Thank you for this awesome resource!
We all gotta start somewhere 😃
For single methods without argument, you can use shorthand syntax (aka. "passing a proc"):
You can also define your own methods and do something similar using
&method
.A lot of these convenience methods work on any
Enumerator
, and a lot of things emit those. You can even write your own with a few lines of code.YES! One of my favorite ruby methods currently is to_enum so you can use all these convenience methods with your own.
I originally had this post titled "Level Up Your Ruby Skillz: Working with Enumerators" but wanted to make it very approachable to new devs learning Ruby and figured starting with straight Arrays would be better.
I didn't know about the buddy method enum_for which also looks super handy.
Everything in Ruby is an object, but maybe we should also say that everything, with the right attitude (or method call!), can be an Enumerator, too.
This is such a useful reference! Thank you for this, it's always great to be reminded how awesome Ruby is! 💙
Ruby's plethora of built-in array methods of one of the best things about the language!
Thanks for this round up ❤️
Thanks MOLLY for sharing it was very useful to me :)
Brilliant article and really useful resource to keep coming back to. Thank you!