From: Patrick Doyle Date: 2008-12-24T02:14:11+09:00 Subject: Re: Loading up a hash with variables if they are defined ------=_Part_55982_23926786.1230052642451 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Tue, Dec 23, 2008 at 11:57 AM, James Coglan wrote: > > > > # Return a hash of the navigation parameters > > def nav_params > > params = {} > > > > params[:prefix] = @prefix if @prefix > > params[:category_id] = @category_id if @category_id > > params[:page] = @page if @page > > > > params > > end > > > > I want to load a parameters hash with the contents of some instance > > variables, but only if they are defined. > > > > params = [:prefix, :category_id, :page].inject({}) do |hash, name| > var = instance_variable_get("@#{name}") > hash[name] = var if var > hash > end > Thanks... that's certainly along the lines of what I was searching for. I'll go read about #instance_variable_get now. --wpd ------=_Part_55982_23926786.1230052642451--