I have some code the checks whether some text is on the HTML page
@@log.test_results(ie.contains_text(‘Dashboard’)
I should get a TRUE (it’s there), but it is returning the figure 62.
Is anything > 0 in Ruby true? If so, why, and shouldn’t the object
automatically change to boolean?
My xml then is falling through
if result == TRUE then
test_status = @test.add_element ‘teststatus’
test_status.text = ‘PASS’
Not knowing anything about what ie.contains_text does, it’s hard for me
to say why it might return 62 instead of TRUE. What is TRUE, anyway? Is
it the same as “true”, that is, the global value that is the single
instance of TrueClass?
In Ruby, everything except false and nil are true. That is, all numbers
including 0 are true, as are all strings including “”. The global value
“false” is the single instance of FalseClass, and the global value “nil”
is the single instance of NilClass.
if result == TRUE then
test_status = @test.add_element ‘teststatus’
test_status.text = ‘PASS’
There’s only two falses in Ruby: false and nil. Everything else is
considered true.
Please note also, that it is usually a very bad idea to try to compare a
boolean value with a boolean constant in a boolean context. Just use
the value or use “!” or “not” to negate. This applies to all sorts of
programming languages (just think of doing “if ( x == TRUE )” in C…
I should get a TRUE (it’s there), but it is returning the figure 62.
considered true.
Please note also, that it is usually a very bad idea to try to compare a
boolean value with a boolean constant in a boolean context. Just use
the value or use “!” or “not” to negate. This applies to all sorts of
programming languages (just think of doing “if ( x == TRUE )” in C…
Kind regards
robert
as Robert has said, its generally bad to say if x==true
in this case contains_text does a reg exp on the html of the page, so
it returns either the start position of the string ( as an integer) or
nil
Because converting a boolean to a boolean by means of comparison with
boolean values
a) is superfluous
b) can do more harm than good (as I pointed out, there are often
multiple values for either “true” or “false” in computer languages, so
comparing with just one of these values will almost certainly make your
program fail at some point).
In particular, consider that none of the following are the boolean value
true:
‘true’
1
23
[ true ]
Ruby’s answer is just to say that an expression has truthiness if it’s
not nil or false. (Lua, I think, does the same thing; certainly, in
lua,
if (0) … end will execute.) C’s answer is to say that an expression
has truthiness if it doesn’t compare equal to zero; this makes null
pointers
and 0-valued ints “false”. I think some languages do it by coercing
everything to boolean.
That sounds really slick, at first! Not only do you get a nice simple
rule,
but you can define your own truth values! AWESOME!
Two problems:
Pretty high performance cost to coerce everything to boolean, given
that 99% of all comparisons ever will be against objects that used the
default rule anyway – especially in Ruby, where it’d be method call
overhead.
You can define your own truth values. That means that any boolean
test
on an object could conceivably be overridden by some joker three
cubicles
away with some idiotic hack to override a particular object’s
hypothetical
to_bool method.
As is, for the very easy cases (bools, testing whether a variable exists
at
all) you get the results you want, and if you want a specific test, you
can
perform it.
Advantage: Ruby.
(And I say this as someone who virtually NEVER writes ‘if (ptr == NULL)’
in C,
because I think it’s gratuitously verbose. ‘if (ptr)’ seems clearer to
me.)
(And I say this as someone who virtually NEVER writes ‘if (ptr == NULL)’ in C,
because I think it’s gratuitously verbose. ‘if (ptr)’ seems clearer to me.)
Just keep in mind that the behavior of the program will be dramatically
different if you replace “if (ptr == NULL)” with “if (ptr)”. :-))
as Robert has said, its generally bad to say if x==true
Not sure why it would be bad practice to compare to true?
Because converting a boolean to a boolean by means of comparison with
boolean values
a) is superfluous
b) can do more harm than good (as I pointed out, there are often
multiple values for either “true” or “false” in computer languages, so
comparing with just one of these values will almost certainly make your
program fail at some point).
(And I say this as someone who virtually NEVER writes ‘if (ptr == NULL)’ in C,
because I think it’s gratuitously verbose. ‘if (ptr)’ seems clearer to me.)
Just keep in mind that the behavior of the program will be dramatically
different if you replace “if (ptr == NULL)” with “if (ptr)”. :-))
Pshaw! I reject your naive dichotomy between true and false. I’m
trying
to undermine the patriarchal social contract that subjects code to
“execution”, by rejecting the Hegelian dichotomy. BTW, I think anyone
who hasn’t yet read Jacques Derrida’s “C by Deconstruction” should, as
soon as someone gets around to writing it.
Pshaw! I reject your naive dichotomy between true and false. I’m trying
to undermine the patriarchal social contract that subjects code to
“execution”, by rejecting the Hegelian dichotomy. BTW, I think anyone
who hasn’t yet read Jacques Derrida’s “C by Deconstruction” should, as
soon as someone gets around to writing it.
-s
Stay calm, breath by your nose and do not panic by any means.
Help is on the way.
Pshaw! I reject your naive dichotomy between true and false. I’m trying
to undermine the patriarchal social contract that subjects code to
“execution”, by rejecting the Hegelian dichotomy.
Next you’ll claim that black is white and Ruby is faster than.
Only if someone pays me to.
Certainly not Derrida as he has been - um - deconstructed already.