Simple If

P

Pete Moran

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!
 
P

Pete Moran

UPDATE:

This works

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

But is there a oneliner equivalent?
 
A

Anthony Eden

UPDATE:

This works

=A0 =A0if params[:country][:id]
=A0 =A0 =A0id =3D params[:country][:id]
=A0 =A0else
=A0 =A0 =A0id =3D params[:id]
=A0 =A0end

But is there a oneliner equivalent?

id =3D params[:country][:id] || params[:id]

--=20
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
 
R

Robert Dober

UPDATE:

This works

=A0 =A0if params[:country][:id]
=A0 =A0 =A0id =3D params[:country][:id]
=A0 =A0else
=A0 =A0 =A0id =3D params[:id]
=A0 =A0end

But is there a oneliner equivalent?

id =3D params[:country][:id] || params[:id]
maybe safer but still not safe

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

--=20
module Kernel
alias_method :=EB, :lambda
end
 
R

Robert Klemme

2009/8/13 Robert Dober said:
UPDATE:

This works

=A0 =A0if params[:country][:id]
=A0 =A0 =A0id =3D params[:country][:id]
=A0 =A0else
=A0 =A0 =A0id =3D params[:id]
=A0 =A0end

But is there a oneliner equivalent?

id =3D params[:country][:id] || params[:id]
maybe safer but still not safe

id =3D params[:country] && params[:country][:id] || params[:id]

I'd go for

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

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

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

For me that translates to something like this:

def any_method(some_id_argument)
id =3D @hash[:id] || some_id_argument
...
end

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Robert Dober

2009/8/13 Robert Dober said:
UPDATE:

This works

=C2=A0 =C2=A0if params[:country][:id]
=C2=A0 =C2=A0 =C2=A0id =3D params[:country][:id]
=C2=A0 =C2=A0else
=C2=A0 =C2=A0 =C2=A0id =3D params[:id]
=C2=A0 =C2=A0end

But is there a oneliner equivalent?

id =3D params[:country][:id] || params[:id]
maybe safer but still not safe

id =3D params[:country] && params[:country][:id] || params[:id]

I'd go for

id =3D (params[:country] || params)[:id] that is nicely refactored

But, this does not seem to be what OP needs. =C2=A0The Perl code was

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

For me that translates to something like this:

def any_method(some_id_argument)
=C2=A0id =3D @hash[:id] || some_id_argument
not really, rather
id =3D @hash.fetch( :id, some_id_argument)
or the fancier
id =3D @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, "<b>AGAIN</b>" } ;)

HTH
Robert
=C2=A0...
end

Kind regards

robert



--=20
module Kernel
alias_method :=CE=BB, :lambda
end
 
R

Robert Klemme

2009/8/13 Robert Dober said:
2009/8/13 Robert Dober said:
UPDATE:

This works

=A0 =A0if params[:country][:id]
=A0 =A0 =A0id =3D params[:country][:id]
=A0 =A0else
=A0 =A0 =A0id =3D params[:id]
=A0 =A0end

But is there a oneliner equivalent?

id =3D params[:country][:id] || params[:id]
maybe safer but still not safe

id =3D params[:country] && params[:country][:id] || params[:id]

I'd go for

id =3D (params[:country] || params)[:id] that is nicely refactored

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

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

For me that translates to something like this:

def any_method(some_id_argument)
=A0id =3D @hash[:id] || some_id_argument
not really, rather
=A0 id =3D @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... :)
or the fancier
=A0 id =3D @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
[email protected]( :id )
rescue KeyNotFoundIBelieve
=A0 raise OMGWhatDidYouDo
end

becomes

@hash.fetch( :id ){ raise OMGWhatDidYouDo, "<b>AGAIN</b>" } ;)

If that should be general behavior then I'd do

@hash =3D Hash.new { raise OMGWhatDidYouDo, "<b>AGAIN</b>" }

elsewhere and then just

id =3D @hash[:id]

It depends on whether you want the same behavior for all misses or just som=
e.

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Robert Dober

2009/8/13 Robert Dober said:
2009/8/13 Robert Dober <[email protected]>:
UPDATE:

This works

=A0 =A0if params[:country][:id]
=A0 =A0 =A0id =3D params[:country][:id]
=A0 =A0else
=A0 =A0 =A0id =3D params[:id]
=A0 =A0end

But is there a oneliner equivalent?

id =3D params[:country][:id] || params[:id]
maybe safer but still not safe

id =3D params[:country] && params[:country][:id] || params[:id]

I'd go for

id =3D (params[:country] || params)[:id] that is nicely refactored

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

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

For me that translates to something like this:

def any_method(some_id_argument)
=A0id =3D @hash[:id] || some_id_argument
not really, rather
=A0 id =3D @hash.fetch( :id, some_id_argument)

Good point! =A0Even though I brought up Hash#fetch in a recent thread I
forgot it this time. =A0Darn, my memory... :)
or the fancier
=A0 id =3D @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
[email protected]( :id )
rescue KeyNotFoundIBelieve
=A0 raise OMGWhatDidYouDo
end

becomes

@hash.fetch( :id ){ raise OMGWhatDidYouDo, "<b>AGAIN</b>" } ;)

If that should be general behavior then I'd do

@hash =3D Hash.new { raise OMGWhatDidYouDo, "<b>AGAIN</b>" }

elsewhere and then just

id =3D @hash[:id]

It depends on whether you want the same behavior for all misses or just s=
ome.
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 =3D Hash::new{ |_, k| raise OMGDidYouForgetToSetThisKey, "violating key =
#{k}" }

R.
 

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