[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newbie : assign a value to a variable...

Nicolas Blanco

8/28/2006 1:22:00 PM

Hi all!

This is really a newbie question...

Here is the simple code...

if @params[:per_page].nil?
per_page = 20
else
per_page = @params[:per_page]
end

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-....

20 Answers

dblack

8/28/2006 1:25:00 PM

0

maikonaraujo@gmail.com

8/28/2006 1:26:00 PM

0

Try this:

per_page = @params[:per_page] || 20

Best Regards
Maikon Araujo.

Nicolas Blanco wrote:
> Hi all!
>
> This is really a newbie question...
>
> Here is the simple code...
>
> if @params[:per_page].nil?
> per_page = 20
> else
> per_page = @params[:per_page]
> end
>
> 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-....

Chris Hulan

8/28/2006 1:27:00 PM

0


Nicolas Blanco wrote:
....
> if @params[:per_page].nil?
> per_page = 20
> else
> per_page = @params[:per_page]
> end
>
> Is there a better way to do this (put the parameter in the variable if
> the parameter is not null, otherwise put 20) ?


The usual idiom is
per_page = @params[:per_page] || 20

cheers

Harold Hausman

8/28/2006 1:29:00 PM

0

On 8/28/06, Nicolas Blanco <slainer68@gmail.com> wrote:
> Hi all!
>
> This is really a newbie question...
>
> Here is the simple code...
>
> if @params[:per_page].nil?
> per_page = 20
> else
> per_page = @params[:per_page]
> end
>
> Is there a better way to do this (put the parameter in the variable if
> the parameter is not null, otherwise put 20) ?
>

#untested:
per_page = (@params[:per_page] or 20)
#The theory is that if @params[:per_page] is nil, it'll return false,
and the 'or' will kick in.

> Thanks...
>

You're welcome.

> Nicolas
>
> --
> Posted via http://www.ruby-....
>

hmmm,
-Harold

Harold Hausman

8/28/2006 1:30:00 PM

0

>
> You can use the || (or) operator:
>
> per_page = @params[:per_page] || 20
>
>
> David
>

I think you've got to be careful about your precedence there...

irb(main):001:0> foo = nil or 20
=> 20
irb(main):002:0> foo
=> nil
irb(main):003:0> foo = (nil or 20)
=> 20
irb(main):004:0> foo
=> 20

Though, maybe it's just irb?

-Harold

James Gray

8/28/2006 1:38:00 PM

0

On Aug 28, 2006, at 8:30 AM, Harold Hausman wrote:

>>
>> You can use the || (or) operator:
>>
>> per_page = @params[:per_page] || 20
>>
>>
>> David
>>
>
> I think you've got to be careful about your precedence there...
>
> irb(main):001:0> foo = nil or 20
> => 20
> irb(main):002:0> foo
> => nil
> irb(main):003:0> foo = (nil or 20)
> => 20
> irb(main):004:0> foo
> => 20
>
> Though, maybe it's just irb?

IRb and Ruby treat this the same, but David didn't use "or" so the
precedence is fine. ;)

James Edward Gray II

Harold Hausman

8/28/2006 1:45:00 PM

0

On 8/28/06, James Edward Gray II <james@grayproductions.net> wrote:
> On Aug 28, 2006, at 8:30 AM, Harold Hausman wrote:
>
> >>
> >> You can use the || (or) operator:
> >>
> >> per_page = @params[:per_page] || 20
> >>
> >>
> >> David
> >>
> >
> > I think you've got to be careful about your precedence there...
> >
> > irb(main):001:0> foo = nil or 20
> > => 20
> > irb(main):002:0> foo
> > => nil
> > irb(main):003:0> foo = (nil or 20)
> > => 20
> > irb(main):004:0> foo
> > => 20
> >
> > Though, maybe it's just irb?
>
> IRb and Ruby treat this the same, but David didn't use "or" so the
> precedence is fine. ;)
>
> James Edward Gray II
>
>

holycrap.

Not to highjack this thread or anything, but what's the difference
between '||' and 'or' ?

Only precedence? Or is 'or' some kind of tricky method?

At least we both gave him working code, I guess... haha.

-Harold

dblack

8/28/2006 1:48:00 PM

0

dblack

8/28/2006 1:48:00 PM

0

Nicolas Blanco

8/28/2006 1:48:00 PM

0

Thanks for the trick.

But the next problem is that if params[:per_page] equals 0 then per_page
equals 0 and I get the error message "must have at least one item per
page" from the Rails paginate option...


unknown wrote:
> Hi --
>
> On Mon, 28 Aug 2006, Nicolas Blanco wrote:
>
>> end
>>
>> Is there a better way to do this (put the parameter in the variable if
>> the parameter is not null, otherwise put 20) ?
>
> You can use the || (or) operator:
>
> per_page = @params[:per_page] || 20
>
>
> David


--
Posted via http://www.ruby-....