[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Best practice for set-and-test idiom?

Kevin Nolan

8/14/2008 5:52:00 PM

I've been using Ruby/Rails for about about four months and wonder what
the the Ruby alternative to the popular C/C++ idiom of setting and
testing a variable in the condition of an 'if' statement. If C/C++ the
usage is typically:

if ((ptr = fcn_returning_ptr(...)) == NULL) {
val = ptr->ref
} else {
val = DEFAULT
}

or the compact version:

val = (ptr = fcn_returning_ptr(...)) ? ptr->ref : DEFAULT

Which, when I translate to Ruby yields (ActiveRecord example):

unless (ar = Model.find(...)).nil?
val = ar.attribute
else
val = DEFAULT

or the equivalent expression.

It seems to me that this is very unRuby-like and I don't see any similar
usages in any of the code-bases in which I have looked.

Will someone please provide a counter-example?

Thanks,

kevin nolan
--
Posted via http://www.ruby-....

7 Answers

Tim Hunter

8/14/2008 6:03:00 PM

0

Kevin Nolan wrote:
(snip)
> or the compact version:
>
> val = (ptr = fcn_returning_ptr(...)) ? ptr->ref : DEFAULT
>
> Which, when I translate to Ruby yields (ActiveRecord example):
>
> unless (ar = Model.find(...)).nil?
> val = ar.attribute
> else
> val = DEFAULT
>
> or the equivalent expression.
>
> It seems to me that this is very unRuby-like and I don't see any similar
> usages in any of the code-bases in which I have looked.
>
> Will someone please provide a counter-example?

Ruby has the ternary ?: operator, too:

val = (ar = Model.find(...)) ? ar.attribute : DEFAULT
--
Posted via http://www.ruby-....

Kevin Nolan

8/14/2008 7:39:00 PM

0

Tim Hunter wrote:
> Kevin Nolan wrote:
> (snip)
>> or the compact version:
>>
>> val = (ptr = fcn_returning_ptr(...)) ? ptr->ref : DEFAULT
>>
>> Which, when I translate to Ruby yields (ActiveRecord example):
>>
>> unless (ar = Model.find(...)).nil?
>> val = ar.attribute
>> else
>> val = DEFAULT
>>
>> or the equivalent expression.
>>
>> It seems to me that this is very unRuby-like and I don't see any similar
>> usages in any of the code-bases in which I have looked.
>>
>> Will someone please provide a counter-example?
>
> Ruby has the ternary ?: operator, too:
>
> val = (ar = Model.find(...)) ? ar.attribute : DEFAULT

Tim,

I know -- I just omitted it for the sake of brevity. Do you have any
thoughts about a better, or my ruby-like technique?


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

Tim Hunter

8/14/2008 9:41:00 PM

0

Kevin Nolan wrote:
(snip)
> I know -- I just omitted it for the sake of brevity. Do you have any
> thoughts about a better, or my ruby-like technique?

Actually I suck at these kinds of stylistic questions. If I want
somebody else (where "somebody else" includes "me, after a few months
when I've forgotten what I was doing") to be able to easily understand
my code I use the "long" version with no short-cuts. If it's just for me
I stick with the first thing that works.

--
RMagick: http://rmagick.ruby...

Gavin Sinclair

8/15/2008 2:12:00 PM

0

On Aug 15, 3:51 am, Kevin Nolan <kpno...@insgraph.com> wrote:
>
>      unless (ar = Model.find(...)).nil?
>         val = ar.attribute
>      else
>         val = DEFAULT
>
>    or the equivalent expression.
>
> It seems to me that this is very unRuby-like and I don't see any similar
> usages in any of the code-bases in which I have looked.
>
> Will someone please provide a counter-example?

Does the Model.find(...) need to be buried in a test condition? If
it's retrieving worthwhile data, it's worth a line of its own.

model = Model.find(...)
val = (model && model.attribute) || DEFAULT

The second line could be called idiomatic Ruby. Whether it appeals to
you or not, I can't predict :)

For situations where more complex processing is required before
setting the variable, I am a _big_ fan of these:

val =
if condition then
processing
processing
value1
else
processing
value2
end

val =
case x
when 1: ...
when 2: ...
...
end

Cheers,
Gavin

ara.t.howard

8/15/2008 3:19:00 PM

0

[Note: parts of this message were removed to make it a legal post.]


On Aug 15, 2008, at 8:12 AM, Gavin Sinclair wrote:

> Does the Model.find(...) need to be buried in a test condition? If
> it's retrieving worthwhile data, it's worth a line of its own.

spoken like someone who has debugged code from logs more than once ;-)

>
>
> model = Model.find(...)
> val = (model && model.attribute) || DEFAULT
>
> The second line could be called idiomatic Ruby. Whether it appeals to
> you or not, I can't predict :)
>
> For situations where more complex processing is required before
> setting the variable, I am a _big_ fan of these:
>
> val =
> if condition then
> processing
> processing
> value1
> else
> processing
> value2
> end
>
> val =
> case x
> when 1: ...
> when 2: ...
> ...
> end


me too - the indent is a great indicator. sometimes i even do

val = (
if condition then
processing
processing
value1
else
processing
value2
end
)

for more visual distinction

cheers.

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Phlip

8/15/2008 3:30:00 PM

0

ara.t.howard wrote:

>> Does the Model.find(...) need to be buried in a test condition? If
>> it's retrieving worthwhile data, it's worth a line of its own.
>
> spoken like someone who has debugged code from logs more than once ;-)

Can Ruby report an error's column? Or only its line number?

ara.t.howard

8/15/2008 3:38:00 PM

0


On Aug 15, 2008, at 9:32 AM, Phlip wrote:

> Can Ruby report an error's column? Or only its line number?


the line

which.is.why.this.is.crazy.and.inhumane.to.operators

;-)

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama