Dec 23, 2017, 3:13:25 PM
ruby methods


It’s easy to use. Just call it on the object, then pass tap a block with the code that you wanted to run. The object will be yielded to the block and then be returned.


I don’t know about you, but I look through lots of arrays for data. Ruby enumerables make it easy to find what I need; select, reject, and find are valuable tools that I use daily. But when the dataset is big, I start to worry about the length of time it will take to go through all those records.


When dealing with relational data, sometimes we need to collect a bunch of unrelated attributes and return them in an array that is not nested. with a Block

Using the Array#new with a block pattern, you can create all kinds of bizarre arrays with default data and any amount of nesting.


let’s look at how it behaves for Fixnums. If you call 5<=>5, it returns 0. If you call 4<=>5, it returns -1. If you call 5<=>4, it returns 1. Basically, if the two numbers are the same, it returns 0, otherwise it returns -1 for least to greatest sorting and 1 for reverse sorting.

You can use the spaceship in your own classes by including the comparable module and redefining <=> with logic branching to make it return -1, 0, and 1 for the cases you want.
Five Ruby Methods You Should Be Using