Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of ruby method call is Array(x)

What is the meaning, and where is the Ruby documentation for the syntax of:

Array(phrases)

which I found browsing the Rails source here:

# File actionpack/lib/action_view/helpers/text_helper.rb, line 109
...
119:           match = Array(phrases).map { |p| Regexp.escape(p) }.join('|')

I thought that Array.new would normally be used to create an array, so something different must be going on here. BTW from the context around this code, the phrases variable can be either a string or an array of strings.

like image 310
John Rees Avatar asked Dec 03 '22 16:12

John Rees


1 Answers

It's most likely the Kernel#Array method, see here. It's slightly different than Array.new; it's more of a cast into an array. (It tries to_ary and to_a.)

like image 65
Brian Carper Avatar answered Dec 24 '22 16:12

Brian Carper