T
Terry Michaels
As I learn Ruby, I find a lot of flexibility in the syntax. I was
thinking there was a way to do something like so: Say I had a method
that would receive an array. Is it possible to define the method so that
it can take the array elements /either/ as an Array object, /or/ as a
comma-separated (variable) list of elements (without the brackets)?
Like,
process_data(string1, string2, string3, ...)
would be the same as
string_array = [string1, string2, string3, ...]
process_data(string_array)
I thought something like so would work:
def process_data(*strings)
...
end
That works with the comma-separated list, because the list gets combined
into one array ("strings"), but if I pass in an array object, that array
object is not split up, but rather becomes one (Array object) element of
the "strings" array.
Do I need to write code that checks whether each object passed in is an
array or not, manually splitting if necessary (which perhaps is
complicated) or is there something else I'm overlooking? Or perhaps is
my whole idea dumb from the start...?
thinking there was a way to do something like so: Say I had a method
that would receive an array. Is it possible to define the method so that
it can take the array elements /either/ as an Array object, /or/ as a
comma-separated (variable) list of elements (without the brackets)?
Like,
process_data(string1, string2, string3, ...)
would be the same as
string_array = [string1, string2, string3, ...]
process_data(string_array)
I thought something like so would work:
def process_data(*strings)
...
end
That works with the comma-separated list, because the list gets combined
into one array ("strings"), but if I pass in an array object, that array
object is not split up, but rather becomes one (Array object) element of
the "strings" array.
Do I need to write code that checks whether each object passed in is an
array or not, manually splitting if necessary (which perhaps is
complicated) or is there something else I'm overlooking? Or perhaps is
my whole idea dumb from the start...?