Simple If

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!

···

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

UPDATE:

This works

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

But is there a oneliner equivalent?

···

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

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

···

On Wed, Aug 12, 2009 at 10:14 PM, Pete Moran<pete@zoborg.com> wrote:

UPDATE:

This works

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

But is there a oneliner equivalent?

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

maybe safer but still not safe

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

···

On Thu, Aug 13, 2009 at 10:18 AM, Anthony Eden<anthonyeden@gmail.com> wrote:

On Wed, Aug 12, 2009 at 10:14 PM, Pete Moran<pete@zoborg.com> wrote:

UPDATE:

This works

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

But is there a oneliner equivalent?

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

--
module Kernel
  alias_method :λ, :lambda
end

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

···

2009/8/13 Robert Dober <robert.dober@gmail.com>:

On Thu, Aug 13, 2009 at 10:18 AM, Anthony Eden<anthonyeden@gmail.com> wrote:

On Wed, Aug 12, 2009 at 10:14 PM, Pete Moran<pete@zoborg.com> wrote:

UPDATE:

This works

if params[:country][:id]
id = params[:country][:id]
else
id = params[:id]
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]

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

UPDATE:

This works

if params[:country][:id]
id = params[:country][:id]
else
id = params[:id]
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]

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 :wink: 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>" } :wink:

HTH
Robert

···

On Thu, Aug 13, 2009 at 5:00 PM, Robert Klemme<shortcutter@googlemail.com> wrote:

2009/8/13 Robert Dober <robert.dober@gmail.com>:

On Thu, Aug 13, 2009 at 10:18 AM, Anthony Eden<anthonyeden@gmail.com> wrote:

On Wed, Aug 12, 2009 at 10:14 PM, Pete Moran<pete@zoborg.com> wrote:

...
end

Kind regards

robert

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

--
module Kernel
  alias_method :λ, :lambda
end

UPDATE:

This works

if params[:country][:id]
id = params[:country][:id]
else
id = params[:id]
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]

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)

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

or the fancier
id = @hash.fetch( :id ){ some_id_argument }

just for completeness :wink: 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>" } :wink:

If that should be general behavior then I'd do

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

elsewhere and then just

id = @hash[:id]

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

Cheers

robert

···

2009/8/13 Robert Dober <robert.dober@gmail.com>:

On Thu, Aug 13, 2009 at 5:00 PM, Robert > Klemme<shortcutter@googlemail.com> wrote:

2009/8/13 Robert Dober <robert.dober@gmail.com>:

On Thu, Aug 13, 2009 at 10:18 AM, Anthony Eden<anthonyeden@gmail.com> wrote:

On Wed, Aug 12, 2009 at 10:14 PM, Pete Moran<pete@zoborg.com> wrote:

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

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.

···

On Fri, Aug 14, 2009 at 9:02 AM, Robert Klemme<shortcutter@googlemail.com> wrote:

2009/8/13 Robert Dober <robert.dober@gmail.com>:

On Thu, Aug 13, 2009 at 5:00 PM, Robert >> Klemme<shortcutter@googlemail.com> wrote:

2009/8/13 Robert Dober <robert.dober@gmail.com>:

On Thu, Aug 13, 2009 at 10:18 AM, Anthony Eden<anthonyeden@gmail.com> wrote:

On Wed, Aug 12, 2009 at 10:14 PM, Pete Moran<pete@zoborg.com> wrote:

UPDATE:

This works

if params[:country][:id]
id = params[:country][:id]
else
id = params[:id]
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]

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)

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

or the fancier
id = @hash.fetch( :id ){ some_id_argument }

just for completeness :wink: 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>" } :wink:

If that should be general behavior then I'd do

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

elsewhere and then just

id = @hash[:id]

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