From: "Booker C.Bense" Date: 2006-10-13T23:20:10+09:00 Subject: Re: Ruby expressions vs. values (like Perl?) -----BEGIN PGP SIGNED MESSAGE----- In article <65e0bb520610121750k33171391q78d868bdaa1f8efe@mail.gmail.com>, John Gabriele wrote: >On 10/12/06, Booker C. Bense > wrote: >> >> One of the nice things about Ruby is that you can always ask it >> what it thinks. >> >> irb(main):090: bar.class >> => Array > >Right. I see that bar is an Array, but I what I meant was, what kind >of expression is that comma-separated thingy on the right hand side >(when I made the assignment to bar)? Evidently, the answer seems to be >that ruby is allowing a little bit of extra syntactic sugar to make it >so I don't have to type those square brackets. Yes. For good or ill, Ruby has various syntactic sugar bits sprinkled about. > >> > >> >Incidentally, I also note that this one -- although performing >> >parallel assignment -- also happens to be generating an Array value: >> > >> >irb(main):003:0> zz = ( a, b, c = 'gee', 'aitch', 'aye' ) >> >=> ["gee", "aitch", "aye"] >> > >> >> It's not making an Array, it's making a reference to an existing >> object. > >Huh? irb seems to be clearly showing an Array. I'm thinking it's like >in C++ where you have this little short-lived temporary object. It >seems like it's an extra step for the interpreter to bother making >that extra temporary -- I'd think it could've just done the assignment >to a, b, and c and be done with it. This is the part of Ruby that took me the longest to get my head around and I think causes lot's of difficulties since it uses a reference model that's quite different from many other languages. Variables are always a reference to an object, assignment copies the reference but not the object. In many languages you can do a = b and then manipulate a , but b will remain unchanged. In Ruby, both b and a are pointers to the underlying object and if you change a then b changes as well. There is only 1 Array object in the fragment above. In Ruby if you want a more conventional version of assignment you do a = b.copy _ Booker C. Bense -----BEGIN PGP SIGNATURE----- Version: 2.6.2 iQCVAwUBRS+f7GTWTAjn5N/lAQEjEgP+IWgJY5cyaJOb1Xp1I7t0kJulonMPuqKY Vj9wXYNgS05jKuGz0xRY8EVcMHopJxELn0+Zij0cknAYeEkSn+Balnnw3kJk+5oi Ao6w/9nK1eRZ4tOFcXBMqHyMedHlavKXBm+BIIJDJHH9COTJkFB/0lN/qRkCc7cE Ovaey9QPQhs= =vY74 -----END PGP SIGNATURE-----