in reply to Re: Honest question about Perl, Python and Ruby
in thread Honest question about Perl, Python and Ruby

Perl has a notable advantage for doing arbitrary data structures

I wonder if this advantage is due to autovivification or are there other reasons? AFAIK, autovivification is unique to Perl. A simple illustration of autovivification in Perl vs Ruby (taken from this old golf node) is:

$z[ 0][ 8] = 'o'; $z[ 1][11] = 'o'; $z[ 3][14] = 'o'; $z[ 5][15] = 'o'; $z[ 7][14] = 'o'; $z[ 9][11] = 'o'; $z[10][ 8] = 'o'; $z[ 9][ 4] = 'o'; $z[ 7][ 1] = 'o'; $z[ 5][ 0] = 'o'; $z[ 3][ 1] = 'o'; $z[ 1][ 4] = 'o'; print"@$_\n"for@z;

To emulate the Perl test program above in Ruby, you might try:

z=[] (z[ 0]||=[])[ 8] = 'o' (z[ 1]||=[])[11] = 'o' (z[ 3]||=[])[14] = 'o' (z[ 5]||=[])[15] = 'o' (z[ 7]||=[])[14] = 'o' (z[ 9]||=[])[11] = 'o' (z[10]||=[])[ 8] = 'o' (z[ 9]||=[])[ 4] = 'o' (z[ 7]||=[])[ 1] = 'o' (z[ 5]||=[])[ 0] = 'o' (z[ 3]||=[])[ 1] = 'o' (z[ 1]||=[])[ 4] = 'o' puts z.map{|i|(i||[]).join" "}
Note that, because Ruby does not autovivify, you must manually create the empty lists -- using the || operator in the test program above. Note further that 0 and "" evaluate to true in Ruby, so the Ruby || operator is closer to Perl's // "defined or" operator than its || "or" operator.

Update: As noted in brian_katzung's necropost reply below, you can autovivify nowadays in Ruby using his XKeys Gem. Some links:

Replies are listed 'Best First'.
Re^3: Honest question about Perl, Python and Ruby
by brian_katzung (Initiate) on Jul 14, 2019 at 02:23 UTC

    You can autovivify in Ruby with my XKeys Gem.

    require 'xkeys' z = [].extend XKeys::Auto z[0, 8] = z[1, 11] = ... = z[1, 4] = 'o'

    And

    node_info = {}.extend XKeys::Auto node_info['list', :[], 'name'] = 'new node' # :[] means next index # {"list"=>[{"name"=>"new node"}]}