From: Robert Dober Date: 2006-08-28T23:25:38+09:00 Subject: Re: Newbie : assign a value to a variable... ------=_Part_172497_1974996.1156775134393 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 8/28/06, Nicolas Blanco wrote: > > Hi all! > > This is really a newbie question... > > Here is the simple code... > > if @params[:per_page].nil? > per_page =3D 20 > else > per_page =3D @params[:per_page] > end You got very bright answers from very good people, or was it the other way round? The answers were of type var =3D val || default_val As it seems that you are accessing a Hash object, you could do the followin= g which is semantically different and this is the main reason I anwser your post. per_page =3D @params.fetch( :per_page, 20 ) Now where is the difference: if @params does not contain the key :per_page, 20 is returned, however if @params[:per_page] was set to nil or false, nil or false is returned accordingly It is up to you to know what you need :) Hope that helps Robert Is there a better way to do this (put the parameter in the variable if > the parameter is not null, otherwise put 20) ? > > Thanks... > > Nicolas > > -- > Posted via http://www.ruby-forum.com/. > > --=20 Deux choses sont infinies : l'univers et la b=EAtise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------=_Part_172497_1974996.1156775134393--