Ruby vs Perl Arrays
One thing that you take for granted in perl is the interchangeability of lists and Arrays. That is, when you want a list, you can easily use an Array type, you may have to dereference that mutha, but you’re going to be able to use it and use it very conveniently. So, if you’re trying to compose a new list or array out of other lists and arrays it’s easy and obvious, you just do it. So in perl you can say:
some_func( 'arg1', 'arg2', @my_array, 'arg3' );
and all the elements of @my_array will be passed individually to some func. As far as I can tell in Ruby that isn’t really possible unless your array happens to have the last elements of the argument list, in which case you could do something like:
some_func( 'arg1', 'arg2', 'arg3', *my_array )
which would expand out your array as individual list elements.
Even in composing arrays, you need to first compose it the way you would in perl and then call the flatten method on that list in order to make it what you want. But here, if you are composing nested arrays and don’t want to flatten everything, you’ll need to do some crazy stuff to get things the way you’d like.
I think the disconnect comes because a perl array is very close to a perl list and it nests things by using references, in Ruby arrays have nothing to do with lists and they don’t like to be mixed. Ruby doesn’t even really talk about lists. The syntax for creating a perl array is the same syntax for creating a list, but in Ruby where everything is an object you create an Array with square brackets. I’m not sure if this is just strange to me because I’m coming from the perl world, but I compose lists and arrays all the time, I hope there’s some Rubyism that I come across that fulfills the same needs.
UPDATE: Here’s someone who’s not so appreciative of perl’s referencing system. You definitely do need to get used to passing arrays by reference, since if you pass an array by value because of it’s list-yness you’ll be copying lots of elements. So, it is a tradeoff in terms of syntactic elements where a Ruby array is much more similar to a Perl array ref, but you get one extra level of convenience since you can deref a perl array ref into a list/array.







