From: "Booker C.Bense" Date: 2006-12-07T04:45:06+09:00 Subject: Re: variable pointers -----BEGIN PGP SIGNED MESSAGE----- In article , xyz wrote: > >I have a situation where I'm walking through an unordered sequence of text >(splitting at newlines) looking for particular patterns. If a particular pattern >matches then I perform some processing unique to that pattern and assign the >output to a particular location (the location could be anything, e.g. local >scalar, a particular array element, an instance variable, etc.). You are thinking in "C", this just doesn't work well in Ruby. Everything in Ruby is an object and every variable is just a pointer to that object. When you say a = b in Ruby, it means in C a = &b and a always evaluates to *a > >WARNING: C syntax approaching. Please restore your seats to their upright and >locked positions and apply seat belts. > >Using "&" (take address) and "*" (de-reference pointer) from C, conceptually >what I want to do is: > > template = [ > [ patternRegex, processingProc, &storage ] > ... > ] > textSequence.each { |line| > template.each { |tuple| > *tuple[2] = tuple[1].call(line) if line =~ tuple[0] > } > } > >Obviously this could be achieved with eval() but that is very heavyweight. > >Are there other options? > Just use tuple[2] = tuple[1].call(line) if line =~ tuple[0] You can't easily pass storage around in Ruby, like you can in C. What you pass around is a pointer to an object. The processingProc should handle the createtion of the object, the storage item is just a name for where you store the pointer to the object. If you are managing storage, rather than objects you are doing things the hard way in Ruby. _ Booker C. Bense -----BEGIN PGP SIGNATURE----- Version: 2.6.2 iQCVAwUBRXccaGTWTAjn5N/lAQH8KwQAvIk0tnWmc1aGr3MYFqfg08CBWXa5cBLw rQRJEfnxMuW9WyaHBikO2Gb+aB2CLKMaMl3IfaTNSUK0aJtzuJpBi2/RUa6Mbz1j EwD5qadlV9ZqQM50buQTUx+gGUVuYjXWSU7HKK0LrFfQlF3cD6Bv47DCuqJNToc7 PvLGw2n0Qns= =FwnP -----END PGP SIGNATURE-----