Introduction
A code block (or simply "block") is an object in the Ruby programming language that holds some Ruby code along with the execution context. The most visually distinctive feature of Ruby is its use of code blocks, which is also one of the features that confuses newcomers from other languages the most. A Ruby code block is essentially an unnamed method.
The majority of other programming languages have constructs akin to a Ruby code block, such as function pointers in C, function objects in C++, lambdas and list comprehensions in Python, anonymous functions in Perl, and anonymous inner classes in Java. Most of these features are hidden away in those languages' obscure corners, avoided by inexperienced programmers. Without code blocks, Ruby cannot be written. Only Lisp is more block-oriented than the other major languages.
Click on the following link to read further: Features of C Language
Writing Block Methods That Classify or Collect
Problem
You require more than just the fundamental block methods provided by the Ruby standard library. You want to create a custom method that either transforms each element in an enumeration (like Enumerable#collect) or classifies the elements in an enumeration (like Enumerable#detect and Enumerable#find all).
Solution
When writing a method that searches or categorises an array of objects, inject is typically an option. By using inject, you can create your own iterations of methods like detect and find all:
module Enumerable
def find_no_more_than(limit)
inject( [] ) do |a,e|
a << e if yield e
return a if a.size >= limit
a
end
end
end
The following code locates the top three even numbers in a list:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a.find_no_more_than(3) { |x| x % 2 == 0 } # => [2, 4, 6]
If you find that you need to create a method like collect, it's probably because collect itself yields elements in the incorrect order for your needs. Because inject returns elements in the same order as collect, you cannot use it.
You must locate or create an iterator that produces elements in the desired order. Once you've done that, you have two choices: either build an Enumerable object using the iterator method and call its collect method, or write a collect equivalent on top of the iterator method.
Discussion
Due to the fact that arrays are the most basic and widely used Enumerable data type, we went into more detail about these block methods. However, almost any data structure can be listed, and a more complex data structure can be listed in a wider variety of ways.
In fact, each of the Enumerable methods, including detect and inject, is implemented. Every component that emerges from the detect and inject methods is given to the code block. If an element meets a set of requirements, it is determined by the yield statement's value.
When an element that matches is found in a method like detect, iteration may end. A technique like find all iterates through all of the elements and gathers the ones that match.
Similar to other methods, collect collects the values returned by the code block in a new data structure and returns the data structure once the iteration is finished. This differs from other methods that return a subset of elements based on what the code block says.
When using an object and wishing the collect method used a different iterator, you should convert the object to an Enumerator and call the collect method on it. To expose a new collect-like method, however, you must define a new method if you're writing a class. As a result, your users will be able to use all of Enumerable's methods, not just collect, by exposing a method that returns a custom Enumerator.