Michael Brooks wrote:
> Hello everyone:
>
> Why can't I name boolean variables with a "?" on the end? The "?" can
> be used on the end of methods but not variable.
>
Thank you everyone for your replies.
I realize that booleans aren't really a type in Ruby and I don't have a
problem with that. It's actually very cool that Ruby is designed to
work that way. Especially considering that variable being used as a
boolean in Ruby can also me set to nil to indicate "answer unknown" like
booleans in databases.
I'm not intending the "?" to control the parser or anything. It's just
"decoration" to remind me that the variable or method is intended to be
treated like a boolean when I'm looking at my code. The "?" suffix is
simpler than a "is_", "has_" prefix or "_flag" suffix for labeling
booleans. I believe one or two Ruby book even recommend that methods
which act like booleans should end with names that have a "?".
So, its unfortunate that I can't put a "?" on the end of variables that
do the same thing. I realize I can wrap a variable in a method but
that's a lot more code just to have boolean variables named the way I
want. And coding that way gets even uglier when I'm only using the
variable privately within a method in which case putting wrappers around
it would look weird.
Oh well... I guess the answer is just "you can't do it" and I'll have to
leave it at that.
Thanks again for your replies.
Michael