Newbie : assign a value to a variable...

N

Nicolas Blanco

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
 
D

dblack

Hi --

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
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
C

ChrisH

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
 
H

Harold Hausman

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.

hmmm,
-Harold
 
H

Harold Hausman

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
 
J

James Edward Gray II

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
 
H

Harold Hausman

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
 
D

dblack

Hi --

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


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
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
N

Nicolas Blanco

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

Hi --

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
 
D

dblack

Hi --

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?

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
http://www.manning.com/black => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.
 
J

James Edward Gray II

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

"and" and "or" have much lower precedence than "&&" and "||", mostly
to allow for natural language constructs like:

res = may_return_nil() or raise "Oops, we got a nil!"

Hope that helps.

James Edward Gray II
 
K

khaines

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
 
J

Jan Svitok

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

Nicolas Blanco

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.

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
 
K

khaines

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

???

Can someone tell me what horrible thing Rails is doing to the language,
there?

How is @params[:per_page].to_i becoming false?


Kirk Haines
 
D

David Vallner

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
 
N

Nicolas Blanco

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,901
Members
47,439
Latest member
elif2sghost

Latest Threads

Top