From: "David A. Black" Date: 2008-05-03T10:36:51+09:00 Subject: Re: How to access to an Array element without doing "find" each time --1926193751-1566499253-1209778611=:29983 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-1566499253-1209778611=:29983" 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-1566499253-1209778611=:29983 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Sat, 3 May 2008, I=C3=B1aki Baz Castillo wrote: > Hi, I've an Array containing complex objects of Header class: > > ------------------------------------- > class Header > =09#attributes: > =09# - @name > =09# - @original_value > =09# - @parsed_value > =09# ... > end > > class Message > =09#attributes: > =09# - @first_line > =09# - @headers =3D > =09# [header_1, header_2, header_3...] > =09# - @body > end > ------------------------------------- > > Just some headers are parsed, and for a header "From", @name attribute ca= n be: > "From", "from", "FROM", "F", "f" ... =3D> /^(from|f)$/i > > > Now I need to access a specifi header ("From" for example) several times = and I > want to avoid doing a "Header.find { |h| h.name =3D~ /^(from|f)$/i }" fo= r each > access,so: > > class Message > =09def header(name) > =09=09Header.find { |h| h.name =3D~ /^(from|f)$/i } > =09end > end > > > But doing message.header("From") for each access will be not very efficie= nt > and I try to avoid it. Is there any other way? > > I could doing an assigment just the first time: > > message.hdr_from =3D message.header("From") > ...do_somethig_with_hdr_from... : > message.hdr_from.parsed_value.uri > message.hdr_from.parsed_value.uri.name > message.hdr_from.parsed_value.display_name > ... > > So "hdr_from" is like a pointer to header("From"), but it's not valid for= me > since I need the possibility of doing a re-assignement: > > header("From") =3D header("To") That's not legal Ruby. A method call isn't an lvalue. > So if after doing that I use "message.hdr_from" I will access to the prev= ious > value of "From" header instead of the new one. > > > In fact, what I'm looking for is a persister pointer, concept that I alre= ady > know doesn't exist in Ruby :( 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 :-) Specifically, I think you should stop thinking in terms of pointers. Ruby doesn't have them, so it's never going to be an exact fit, and it won't take you very far in exploring Ruby techniques. 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. 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-1566499253-1209778611=:29983-- --1926193751-1566499253-1209778611=:29983--