Ruby we might Miss

This is the third part of the series I started.
The topic for this post :


• Enumerable#all? returns true if the given block returns a true value for all items in the
enumerable. Similarly, Enumerable#any? returns true if the block returns a true value
for any item.
(1..10).all?{|i| i > 0 && i true
(1..10).any?{|i| i*i == 9} # => true
(1..10).any?{|i| i*i == 8} # => false

• Enumerable#grep filters an enumerable against another object using ===, affording all of
the usual flexibility of the === method:
[1, 2, 3].methods.grep(/^so/) # => [“sort!”, “sort”, “sort_by”]
[1, :two, “three”, 4].grep(Fixnum) # => [1, 4]

• Enumerable#sort_by sorts the enumerable by the value of the given block, by performing
a Schwartzian transform* on the data. It builds up a set of input elements, each
stored with the result of applying the block to that element. Because the block should
return the same value when called with the same input, it only needs to be called once
per input. Thus, O(n) calculations are done rather than O(n lg n).
However, the sort_by technique is counterproductive when key calculation is inexpensive;
in such cases, Enumerable#sort should be called with a custom comparison as a

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s