Newbie : assign a value to a variable

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-forum.com/.

Hi --

···

On Mon, 28 Aug 2006, 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) ?

You can use the || (or) operator:

   per_page = @params[:per_page] || 20

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

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-forum.com/\.

hmmm,
-Harold

···

On 8/28/06, Nicolas Blanco <slainer68@gmail.com> wrote:

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-forum.com/\.

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

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

You got very bright answers from very good people, or was it the other way
round?

The answers were of type
    var = val || default_val

As it seems that you are accessing a Hash object, you could do the following
which is semantically different and this is the main reason I anwser your
post.

per_page = @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 :slight_smile:

Hope that helps
Robert

Is there a better way to do this (put the parameter in the variable if

···

On 8/28/06, Nicolas Blanco <slainer68@gmail.com> wrote:

the parameter is not null, otherwise put 20) ?

Thanks...

Nicolas

--
Posted via http://www.ruby-forum.com/\.

--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

Nicolas Blanco wrote

Is there a better way to do this (put the parameter in the variable if the parameter is not null, otherwise put 20) ?

The original way works.

Just how much "better" than "works" do you want? I'd say fight the urges to golf.

David Vallner

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

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-forum.com/\.

You can also do:
per_page = @params.fetch(:per_page, 20)

Or:
@params.default = 20
per_page = @params[:per_page]

···

On 28 août 06, at 15:30, ChrisH wrote:

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

--
Luc Heinrich - luc@honk-honk.com - http://www.honk-honk.com

Thank you very much for all your answers...

I think I will use this for now :

  per_page = @params.fetch(:per_page, 20).to_i
  per_page = 20 if per_page <= 0

Nicolas.

···

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

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

James Edward Gray II

···

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?

Hi --

···

On Mon, 28 Aug 2006, 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?

No, it's just "or", which I didn't use. (Look more closely at my code
:slight_smile:

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.

In that case, use the trinary operator. Call to_i on @params[:per_page] if it could be nil, as you don't want to do that comparison with nil.

per_page = @params[:per_page].to_i > 0 ? @params[:per_page] : 20

Kirk Haines

···

On Mon, 28 Aug 2006, Nicolas Blanco wrote:

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

   per_page = @params[:per_page] || 20

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

per_page = @params[:per_page] || 20 # default
per_page = 20 if per_page <= 0 # validity check

or the other way round:

per_page = @params[:per_page] # assignment
per_page = nil if per_page <= 0 # validity check
per_page ||= 20 # default

Choose what you like best.

···

On 8/28/06, Nicolas Blanco <slainer68@gmail.com> wrote:

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:
>
> David

--
Posted via http://www.ruby-forum.com/\.

Thanks for that. I've been oblivious to the default and block options for fetch!

Gary Wright

···

On Aug 28, 2006, at 10:15 AM, Luc Heinrich wrote:

You can also do:
per_page = @params.fetch(:per_page, 20)

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

···

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

James Edward Gray II

Thanks!

I tried :
per_page = @params[:per_page].to_i > 0 ? @params[:per_page] : 20

but in this case if @params[:per_page] = 'blahblah' it works, I get 20
in per_page but if @params[:per_page] = 6 or another integer I get :
undefined method `>' for false:FalseClass

from Rails...

Nicolas.

unknown wrote:

···

On Mon, 28 Aug 2006, Nicolas Blanco wrote:

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

   per_page = @params[:per_page] || 20

In that case, use the trinary operator. Call to_i on @params[:per_page]
if it could be nil, as you don't want to do that comparison with nil.

per_page = @params[:per_page].to_i > 0 ? @params[:per_page] : 20

Kirk Haines

--
Posted via http://www.ruby-forum.com/\.

or get them all at once:

per_page = @params.fetch(:per_page, 0).nonzero? || 20

cheers

Simon

···

-----Original Message-----
From: Jan Svitok [mailto:jan.svitok@gmail.com]
Sent: Monday, August 28, 2006 4:01 PM
To: ruby-talk ML
Subject: Re: Newbie : assign a value to a variable...

On 8/28/06, Nicolas Blanco <slainer68@gmail.com> wrote:
> 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...

per_page = @params[:per_page] || 20 # default
per_page = 20 if per_page <= 0 # validity check

or the other way round:

per_page = @params[:per_page] # assignment
per_page = nil if per_page <= 0 # validity check
per_page ||= 20 # default

Choose what you like best.

Hi --

···

On Mon, 28 Aug 2006, Harold Hausman wrote:

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

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?

Only precedence, as far as I know.

David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
   ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.