[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

ruby and true

aidy

4/30/2007 2:43:00 PM

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

7 Answers

Jano Svitok

4/30/2007 3:02:00 PM

0

On 4/30/07, aidy.lewis@googlemail.com <aidy.lewis@googlemail.com> wrote:
> 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'

Hi,

1. have a look at this:
http://rubyforge.org/pipermail/wtr-general/2005-November/0...
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

Tim Hunter

4/30/2007 3:08:00 PM

0

aidy.lewis@googlemail.com wrote:
> 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
>
>
>
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.

--
RMagick [http://rmagick.rub...]
RMagick Installation FAQ [http://rmagick.rub.../install-faq.html]


Robert Klemme

4/30/2007 3:29:00 PM

0

On 30.04.2007 16:42, aidy.lewis@googlemail.com wrote:
> 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'
>

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

Paul Rogers

4/30/2007 3:43:00 PM

0

On Apr 30, 9:29 am, Robert Klemme <shortcut...@googlemail.com> wrote:
> On 30.04.2007 16:42, aidy.le...@googlemail.com wrote:
>
>
>
> > 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'
>
> 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

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

aidy

5/4/2007 4:46:00 PM

0

Paul Rogers 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


Robert Dober

5/4/2007 9:34:00 PM

0

On 5/4/07, aidy.lewis@googlemail.com <aidy.lewis@googlemail.com> wrote:
> Paul Rogers 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
>
> <snip>
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Robert Klemme

5/7/2007 7:10:00 AM

0

On 04.05.2007 18:46, aidy.lewis@googlemail.com wrote:
> Paul Rogers 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