# Simple If

**URL:** <https://www.ruby-forum.com/t/simple-if/173681>\
**Category:** Ruby\
**Created:** [August 13, 2009, 10:00am UTC](https://www.ruby-forum.com/t/simple-if/173681 "2009-08-13T10:00:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 13, 2009, 10:00am UTC](https://www.ruby-forum.com/t/simple-if/173681/1 "2009-08-13T10:00:36Z")

</div>

Sorry for total newbie question here - in a method a particular id could  
be passed as a variable or as part of the has - how what would be the  
equivalent in ruby of the following Perl ?

my $id = (exists $hash-\>{id}) ? $hash-\>{id} : $\_;

Assuming that in Ruby (well Rails). The ID will either be in  
params[:id] or params[:country][:id] ?

So if params[:country][:id] contains the value I assign that otherwise I  
assign params[:id]

Thanks for helping my small brain!

---

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 13, 2009, 10:14am UTC](https://www.ruby-forum.com/t/simple-if/173681/2 "2009-08-13T10:14:22Z")

</div>

UPDATE:

This works

```
if params[:country][:id]
  id = params[:country][:id]
else
  id = params[:id]
end

```

But is there a oneliner equivalent?

---

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 13, 2009, 10:19am UTC](https://www.ruby-forum.com/t/simple-if/173681/3 "2009-08-13T10:19:23Z")

</div>

On Wed, Aug 12, 2009 at 10:14 PM, Pete M.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> But is there a oneliner equivalent?  
> id = params[:country][:id] || params[:id]

–  
GMU/IT d- s: a32 C++(++++)$ UL@ P— L+(++) !E W+++$ !N o? K? w— !O  
M++ V PS+ PE Y PGP t+ !5 X- R tv b++ DI+ D++ G- e++ h---- r+++ y++++\*\*

[http://anthony.mp](http://anthony.mp)

---

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 13, 2009, 10:25am UTC](https://www.ruby-forum.com/t/simple-if/173681/4 "2009-08-13T10:25:50Z")

</div>

On Thu, Aug 13, 2009 at 10:18 AM, Anthony E.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > But is there a oneliner equivalent?
> 
> id = params[:country][:id] || params[:id]  
> maybe safer but still not safe

id = params[:country] && params[:country][:id] || params[:id]  
Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 13, 2009, 5:03pm UTC](https://www.ruby-forum.com/t/simple-if/173681/5 "2009-08-13T17:03:47Z")

</div>

2009/8/13 Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > end
> > > 
> > > But is there a oneliner equivalent?
> > 
> > id = params[:country][:id] || params[:id]  
> > maybe safer but still not safe
> 
> id = params[:country] && params[:country][:id] || params[:id]

I’d go for

id = (params[:country] || params)[:id]

But, this does not seem to be what OP needs. The Perl code was

my $id = (exists $hash-\>{id}) ? $hash-\>{id} : $\_;

For me that translates to something like this:

def any\_method(some\_id\_argument)  
id = @hash[:id] || some\_id\_argument  
…  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 13, 2009, 6:12pm UTC](https://www.ruby-forum.com/t/simple-if/173681/6 "2009-08-13T18:12:46Z")

</div>

On Thu, Aug 13, 2009 at 5:00 PM, Robert  
Klemme[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > Â Â Â id = params[:id]
> 
> id = (params[:country] || params)[:id]  
> that is nicely refactored
> 
> But, this does not seem to be what OP needs. Â The Perl code was
> 
> my $id = (exists $hash-\>{id}) ? $hash-\>{id} : $\_;
> 
> For me that translates to something like this:
> 
> def any\_method(some\_id\_argument)  
> Â id = @hash[:id] || some\_id\_argument  
> not really, rather  
> id = @hash.fetch( :id, some\_id\_argument)  
> or the fancier  
> id = @hash.fetch( :id ){ some\_id\_argument }

just for completeness 😉 of the API. (This is interesting for some use  
cases as e.g. throwing an exception in the block, thus

begin  
@hash.fetch( :id )  
rescue KeyNotFoundIBelieve  
raise OMGWhatDidYouDo  
end

becomes

@hash.fetch( :id ){ raise OMGWhatDidYouDo, “ **AGAIN** ” } 😉

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 14, 2009, 9:03am UTC](https://www.ruby-forum.com/t/simple-if/173681/7 "2009-08-14T09:03:06Z")

</div>

2009/8/13 Robert D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > > > ```
> > > > > id = params[:country][:id]
> > > > > 
> > > > > ```
> > 
> > def any\_method(some\_id\_argument)  
> > id = @hash[:id] || some\_id\_argument  
> > not really, rather  
> > id = @hash.fetch( :id, some\_id\_argument)

Good point! Even though I brought up Hash#fetch in a recent thread I  
forgot it this time. Darn, my memory… 🙂

> end
> 
> becomes
> 
> @hash.fetch( :id ){ raise OMGWhatDidYouDo, “ **AGAIN** ” } 😉

If that should be general behavior then I’d do

@hash = Hash.new { raise OMGWhatDidYouDo, “ **AGAIN** ” }

elsewhere and then just

id = @hash[:id]

It depends on whether you want the same behavior for all misses or just  
some.

Cheers

robert

---

<div class="post-metadata">

**Author:** ![pmckinstry](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f6c823/32.png) [@pmckinstry](https://www.ruby-forum.com/u/pmckinstry)\
**Post date:** [August 14, 2009, 10:05am UTC](https://www.ruby-forum.com/t/simple-if/173681/8 "2009-08-14T10:05:08Z")

</div>

On Fri, Aug 14, 2009 at 9:02 AM, Robert  
Klemme[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > > > if params[:country][:id]  
> > > > > > id = params[:country] && params[:country][:id] || params[:id]  
> > > > > > For me that translates to something like this:  
> > > > > > id = @hash.fetch( :id ){ some\_id\_argument }  
> > > > > > becomes
> 
> It depends on whether you want the same behavior for all misses or just some.  
> Right, i.e. a hash implementing parameters will have varying behavior,  
> depending if the parameter is optional or not.  
> Maybe we want to add a messgae about the violating key, so elaborate  
> the useful Hash::new pattern for those who are not familiar with it:

@h = Hash::new{ |\_, k| raise OMGDidYouForgetToSetThisKey, “violating key  
#{k}” }

R.
