Ruby and true

Hi,

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’

cheers

aidy

In Ruby, boolean comparisons will always return true unless the value is
nil
or false.

http://en.wikipedia.org/wiki/Boolean_datatype#Ruby

On 4/30/07, [email protected] [email protected] wrote:

My xml then is falling through

if result == TRUE then
test_status = @test.add_element ‘teststatus’
test_status.text = ‘PASS’

Hi,

  1. have a look at this:
    http://rubyforge.org/pipermail/wtr-general/2005-November/004134.html
  2. next time, if you write library name (watir) and version along with
    ruby version may help in getting better answer.
  3. in ruby it’s ‘true’ not ‘TRUE’ (maybe in watir it’s other, I don’t
    know)

Bye,

Jano

In fact only false and nil is false in Ruby.

Cheers back
Nicolai

[email protected] wrote:

My xml then is falling through

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.

On 30.04.2007 16:42, [email protected] wrote:

My xml then is falling through

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…

Kind regards

robert

In message [email protected],
[email protected] wr
ites:

My xml then is falling through

if result == TRUE then

“if result”.

Don’t compare things to true, true is but one of many truths.

Be more philosophical!

-s

On Apr 30, 9:29 am, Robert K. [email protected] wrote:

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

On 5/4/07, [email protected] [email protected] wrote:

Paul R. wrote

as Robert has said, its generally bad to say if x==true

Not sure why it would be bad practice to compare to true?
Maybe because true is just one special value (which also evaluates to
“true”).

If you want to test that x is true and nothing else than it is
excellent practice to write

if x == true then

this however is so rare a semantics that Robert and Paul are
completely correct with their statement, because in 99% of the cases
what you mean is

unless x.nil? || x == false then

which of course can and shall be written as

if x then

HTH
Robert

Paul R. wrote

as Robert has said, its generally bad to say if x==true

Not sure why it would be bad practice to compare to true?

However,

def test_results(result, msg)
if result
test_status = @test.add_element ‘teststatus’
test_status.text = ‘PASS’
else
fail = @test.add_element ‘teststatus’
fail.text = ‘FAIL!’
fail_msg = @test.add_element ‘failmessage’
fail_msg.text = msg
end
end

Would you see this as more appropriate?

cheers

aidy

In message [email protected], Robert K. writes:

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:

  1. 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.
  2. 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.)

-s

On 07.05.2007 09:23, Peter S. wrote:

(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)”. :-))

robert

On 04.05.2007 18:46, [email protected] wrote:

Paul R. wrote

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).

robert

In message [email protected], Robert K. writes:

On 07.05.2007 09:23, Peter S. wrote:

(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.

-s

On 07.05.2007 09:35, Peter S. wrote:

“execution”, by rejecting the Hegelian dichotomy.
Next you’ll claim that black is white and Ruby is faster than.

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.

Certainly not Derrida as he has been - um - deconstructed already. :slight_smile:

robert still chuckling

On 5/7/07, Peter S. [email protected] wrote:

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.

Robert

In message [email protected], Robert K. writes:

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

Certainly not Derrida as he has been - um - deconstructed already. :slight_smile:

http://fafblog.blogspot.com/2004/10/flowers-for-derrida-jacques-derrida.html

(Warning: Not everyone will find it funny.)

-s

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs