This is the Kernel#Array method, as others have already pointed out.
But the Ruby documentation does not take into account the usefulness of this method to simplify the code. Nor does it tell you that objects that do not have a to_ary or to_a are encapsulated in an array.
Array([1,2,3]) -> [1,2,3] Array(1..3) -> [1,2,3] Array({ a: 1, b: 2 }) -> [[:a, 1],[:b,2]] Array("Hello World") -> ["Hello World"] Array(1) -> [1]
All of these Kernel#Array functions allow you to handle typical corner cases with parameters on the same line.
See this code, which is typical of many APIs or DSLs:
This can be simplified using Kernel#Array :
def handle(data) Array(data).each { |d| ... } end
Of course, you need to be careful about providing different types for the data parameter, because the to_ary / to_a may or may not give you what you expect.
source share