From: "Peña, Botp" Date: 2008-09-12T18:34:36+09:00 Subject: Re: pass by reference in each loop From: Xiong Chiamiov [mailto:xiong.chiamiov+ruby_forum@gmail.com] # As you can see, it's currently only those three fields that I need to # parse; I don't want to use extra resources running everything through # parsing. In the future, though, I see a larger number of fields that # need to be parsed, which is what I'm trying to write for. better to give us a sample php code fr what i can guess and gather, i think you need a hash with vars as keys whose values you want to change anytime eg we create 3 vars @a, @b, @c @a=nil #=> nil @b=2 #=> 2 @c="test" #=> "test" we want to save them and their original values to a hash (so we can call each by their var name, so to speak :) vars={@a=>@a,@b=>@b,@c=>@c} #=> {nil=>nil, 2=>2, "test"=>"test"} at this point, you should notice that ruby does not keep the variable name in the hash, but the original object it pointed. so just in case we want the names preserved, we keep/create and save copy of those names, like so vars_name={@a=>"@a",@b=>"@b",@c=>"@c"} #=> {nil=>"@a", 2=>"@b", "test"=>"@c"} (there are other ways like ostructs, attributes, etc.. but this maybe crude as it gets :) now let's try clobbering. let us "change" the value that var @a was referring to vars[@a]="from nil to something" #=> "from nil to something" let's see. vars #=> {nil=>"from nil to something", 2=>2, "test"=>"test"} ah, success. let's try the other vars. vars[@b]= vars[@b]*2 #=> 4 vars #=> {nil=>"from nil to something", 2=>4, "test"=>"test"} vars[@c]= vars[@c].sub "t","b" #=> "best" vars #=> {nil=>"from nil to something", 2=>4, "test"=>"best"} ok. now let's print the var names together w their old and new values vars_name.values.zip(vars.to_a){|pair| p pair} ["@a", [nil, "from nil to something"]] ["@b", [2, 4]] ["@c", ["test", "best"]] #=> nil is that ok? apologies if this is not what you want. kind regards -botp