From: James Coglan Date: 2008-12-24T01:57:57+09:00 Subject: Re: Loading up a hash with variables if they are defined ------=_Part_66172_25077107.1230051997166 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline > > # 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 ------=_Part_66172_25077107.1230051997166--