From: "Jesús Gabriel y Galán" Date: 2008-12-24T02:05:04+09:00 Subject: Re: Loading up a hash with variables if they are defined On Tue, Dec 23, 2008 at 5:51 PM, Patrick Doyle wrote: > Is there a cleaner, more Rubyesque way to do this? > > # 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. > irb(main):005:0> {:prefix => @prefix, :category_id => @category, :page => @page}.reject {|k,v| v.nil?} => {:prefix=>"asdasdf", :category_id=>"cat"} Jesus.