Why email part.body doesn't change?

this code doesn’t work… why?
how can i add that head to that body?

head is the html text i want to add to p (a tmail object)

def injecthead(head,p)
if p.multipart? then
p.parts.each do |pa|
injecthead(head,pa)
end
else
if p.content_type==“text/html” then
if p.body!=nil then
startbodytag=p.body.index("<body")
endbodytag=(p.body[startbodytag…p.body.length]).index(">")
if startbodytag!=nil then
p.body.insert(startbodytag+endbodytag+1,head)

          #i'm sure this code is worked but p.body doesn't change.
          #if i put

altbody=p.body.insert(startbodytag+endbodytag+1,head),
#altbody is the body i want, but if then i write
p.body=altbody, p.body becames nil

        end
      end
    end

  end
end