# Newbie : assign a value to a variable

**URL:** <https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703>\
**Category:** Ruby\
**Created:** [August 28, 2006, 3:21pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703 "2006-08-28T15:21:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 3:21pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/1 "2006-08-28T15:21:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 3:26pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/2 "2006-08-28T15:26:44Z")

</div>

Hi –

On Mon, 28 Aug 2006, Nicolas B. 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

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 3:30pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/3 "2006-08-28T15:30:46Z")

</div>

On 8/28/06, Nicolas B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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) ?

#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/](http://www.ruby-forum.com/).

hmmm,  
-Harold

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/4 "2006-08-28T15:32:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/5 "2006-08-28T15:32:47Z")

</div>

Try this:

per\_page = @params[:per\_page] || 20

Best Regards  
Maikon Araujo.

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 3:34pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/6 "2006-08-28T15:34:47Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 3:38pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/7 "2006-08-28T15:38:50Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 3:46pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/8 "2006-08-28T15:46:56Z")

</div>

On 8/28/06, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/9 "2006-08-28T15:48:57Z")

</div>

Hi –

On Mon, 28 Aug 2006, Harold H. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 3:50pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/10 "2006-08-28T15:50:57Z")

</div>

Hi –

On Mon, 28 Aug 2006, Harold H. wrote:

> > > > 
> > > 
> > > =\> 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?

Only precedence, as far as I know.

David

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 3:52pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/11 "2006-08-28T15:52:58Z")

</div>

On Aug 28, 2006, at 8:45 AM, Harold H. wrote:

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

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/12 "2006-08-28T15:48:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/13 "2006-08-28T15:56:58Z")

</div>

On Mon, 28 Aug 2006, Nicolas B. 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 H.

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 4:02pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/14 "2006-08-28T16:02:59Z")

</div>

On 8/28/06, Nicolas B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 4:02pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/15 "2006-08-28T16:02:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 4:16pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/16 "2006-08-28T16:16:59Z")

</div>

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

> The usual idiom is  
> per\_page = @params[:per\_page] || 20

You can also do:  
per\_page = @params.fetch(:per\_page, 20)

Or:  
@params.default = 20  
per\_page = @params[:per\_page]

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 4:27pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/17 "2006-08-28T16:27:00Z")

</div>

On 8/28/06, Nicolas B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> 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 🙂

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/](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

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 4:39pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/18 "2006-08-28T16:39:03Z")

</div>

On Mon, 28 Aug 2006, Nicolas B. wrote:

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

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 4:47pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/19 "2006-08-28T16:47:05Z")

</div>

Nicolas B. 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 V.

---

<div class="post-metadata">

**Author:** ![budu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b5e925/32.png) [@budu](https://www.ruby-forum.com/u/budu)\
**Post date:** [August 28, 2006, 4:59pm UTC](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703/20 "2006-08-28T16:59:42Z")

</div>

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.

[Next page](https://www.ruby-forum.com/t/newbie-assign-a-value-to-a-variable/66703.md?page=2)
