2008/4/12, I=F1aki Baz Castillo <ibc / aliax.net>:
> 2008/4/12, Joel VanderWerf <vjoel / path.berkeley.edu>:
>
>
>  >  Why do you need a From class that is distinct from Header?
>
>
> Because in an initial parsing I just get a headers array, whiotu
>  inspecting each of them.
>  Later, if I need, I look for some specific headers (From, To...) and
>  need to add some methods to them.

IMHO it is more efficient and less error prone if you create
appropriate instances during initial parsing.  You have all the
information at that point in time plus you you save one additional
pass through the collection.

>  > Depending on the
>  > reason, it might work to make From a module:
>  >
>  >  module From
>  >   # special From methods here
>  >  end
>  >
>  >  headers.each do |h|
>  >   if h.name =3D=3D "From"
>  >     h.extend From
>  >   end
>  >   # or maybe something like:
>  >   # h.extend const_get(h.name)
>  >  end
>
>
> Thanks, this is great! I didn't know "extend" method.

Well, you can even do it during construction:

class Header
  attr_accessor :name, :body
  def initialize(name, body)
    @name =3D name
    @body =3D body
    extend From if /From/ =3D~ name
  end
end

But I'd rather leave the decision to the parsing code.

Cheers

robert

--=20
use.inject do |as, often| as.you_can - without end