Best practice for set-and-test idiom?

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

Kevin N. 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 H. wrote:

Kevin N. 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?

Kevin N. 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.

On Aug 15, 2008, at 8:12 AM, Gavin S. 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 :wink:

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://codeforpeople.com/

On Aug 15, 3:51 am, Kevin N. [email protected] wrote:

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

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

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

:wink:

a @ http://codeforpeople.com/

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

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