K
Kevin Nolan
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
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