From: "David A. Black" Date: 2008-05-03T21:45:10+09:00 Subject: Re: How to access to an Array element without doing "find" each time --1926193751-1895070519-1209818709=:8585 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-1895070519-1209818709=:8585" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-1895070519-1209818709=:8585 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi ... <- private joke :-) On Sat, 3 May 2008, I=C3=B1aki Baz Castillo wrote: > El S=C3=A1bado, 3 de Mayo de 2008, David A. Black escribi=C3=B3: > >>> So "hdr_from" is like a pointer to header("From"), but it's not valid f= or >>> me since I need the possibility of doing a re-assignement: >>> >>> =C2=A0header("From") =3D header("To") >> >> That's not legal Ruby. A method call isn't an lvalue. > > Oh, yes, you are right! > Please, let me know if I understand correctly why: > > header("From") returns a value so I'm trying to do: > value1 =3D value2 > that is not legal, is it? Right; you can't say, for example, "a string" =3D 3 and every method call returns an object. >> My advice is to try to learn to think in Ruby, so that you don't keep >> experiencing frustration because of what you perceive Ruby as not >> having. Believe me, Ruby is definitely feature-rich enough to parse >> headers :-) > > Well, in fact I've already done the parser (using TreeTop magic). But it'= s not > enought with parsing headers (as it would be in HTTP protocol) since I'm > doing a SIP stack and it can be a proxy (so some headers are modified, ne= w > added, some deleted... and the rest of message remains equal and is forwa= rded > to other server. > That's why I need an efficient way of accessing the @header array of Head= er > objets, parse just one of them and keep the whole message as it arrived. > > > >> A lot of Ruby objects need to change their state or contents, without >> changing what object they are. Methods like Array#replace, >> String#replace, and many others, do exactly that. Maybe your message >> objects could "replace" headers with each other, or something like >> that. > > Ok, so you mean that I should avoid using "=3D" and use objects methods t= o > replace their content. Nice to know. You could combine these techniques, actually, if you did something like: class Message def from_header=3D(h) self.from_header.replace(h) end end That's almost pseudo-code, but the point is you could then do: message.from_header =3D message.to_header or whatever. That's the great thing about the "=3D"-methods; they let you use assignment semantics for method calls, and you can of course define the method yourself. David --=20 Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 24-27 London (Skills Matter) See http://www.rubypal.com for details and updates! --1926193751-1895070519-1209818709=:8585-- --1926193751-1895070519-1209818709=:8585--