# Boolean and logging

**URL:** <https://www.ruby-forum.com/t/boolean-and-logging/133179>\
**Category:** Ruby\
**Created:** [March 28, 2008, 4:00pm UTC](https://www.ruby-forum.com/t/boolean-and-logging/133179 "2008-03-28T16:00:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 28, 2008, 4:00pm UTC](https://www.ruby-forum.com/t/boolean-and-logging/133179/1 "2008-03-28T16:00:46Z")

</div>

Hi,

I use Ruby and Watir

I have a method:

def does\_the\_html\_include(target)  
puts “HTML text of #{target} is being verified”  
if target.kind\_of? String  
return $browser.text.include?(target)  
elsif target.kind\_of? Regexp  
return $browser.text.match(target)  
end  
end

I wrap this up in a logger

```
def test_results(result, *msg)
  #make true explicit, everything else I am seeing as a failure
  if result == true
    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

However, when I don’t want HTML text to appear, eg: an error. I want  
to do something better than this

boolean = does\_the\_html\_include(‘You appear to have an active  
account’)  
$test.test\_results(!boolean, ‘already have registered account’)

Any ideas?

Cheers

Aidy

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 28, 2008, 5:30pm UTC](https://www.ruby-forum.com/t/boolean-and-logging/133179/2 "2008-03-28T17:30:48Z")

</div>

On 28.03.2008 15:58, removed\_email\_address@domain.invalid wrote:

> ```
> elsif target.kind_of? Regexp
> return $browser.text.match(target)
> end
> 
> ```
> 
> end
> 
> I wrap this up in a logger
> 
> ```
> def test_results(result, *msg)
> #make true explicit, everything else I am seeing as a failure
> if result == true
> 
> ```

This is a very bad idea, as only false and nil are false. Your code  
above will especially break with the test because the regexp match will  
either return nil or a MatchData object - which both will make “result  
== true” false.

> However, when I don’t want HTML text to appear, eg: an error. I want  
> to do something better than this
> 
> boolean = does\_the\_html\_include(‘You appear to have an active  
> account’)  
> $test.test\_results(!boolean, ‘already have registered account’)

I am not sure what exactly it is that you want to improve. Do you  
dislike the local variable?

def test\_results(msg)  
if yield  
# test ok  
else  
# not ok  
end  
end

test\_results “foo” { does\_the\_html\_include(‘You appear to have an active  
account’) }

Cheers

robert

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 31, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/boolean-and-logging/133179/3 "2008-03-31T17:16:18Z")

</div>

On Mar 28, 9:28 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > if target.kind_of? String
> > if result == true
> > 
> > ```
> 
> This is a very bad idea, as only false and nil are false. Your code  
> above will especially break with the test because the regexp match will  
> either return nil or a MatchData object - which both will make “result  
> == true” false.

Good point.

> > However, when I don’t want HTML text to appear, eg: an error. I want  
> > to do something better than this
> 
> > boolean = does\_the\_html\_include(‘You appear to have an active  
> > account’)  
> > $test.test\_results(!boolean, ‘already have registered account’)
> 
> I am not sure what exactly it is that you want to improve. Do you  
> dislike the local variable?

Yes. I am writing two lines when I should be writing one.

> account’) }

Could you give a brief explanation of this code please?

Aidy

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 1, 2008, 1:06pm UTC](https://www.ruby-forum.com/t/boolean-and-logging/133179/4 "2008-04-01T13:06:07Z")

</div>

On 31 Mar, 16:56, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The method invokes the block and uses the result to determine what to  
> do (if or else).
> 
> Does that help?

Surely does. Thanks Robert, your a linguist.

Aidy

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [March 31, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/boolean-and-logging/133179/5 "2008-03-31T17:57:13Z")

</div>

2008/3/31, removed\_email\_address@domain.invalid  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > def does\_the\_html\_include(target)  
> > > def test\_results(result, \*msg)  
> > > Good point.  
> > > end  
> > > dislike the local variable?
> 
> Yes. I am writing two lines when I should be writing one.

Well, if that’s all that bothers you - you can easily inline it:

$test.test\_results(!does\_the\_html\_include(‘You…’), ‘already have  
registered account’)

> Could you give a brief explanation of this code please?

The method invokes the block and uses the result to determine what to  
do (if or else).

Does that help?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 2, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/boolean-and-logging/133179/6 "2008-04-02T14:15:42Z")

</div>

On 1 Apr, 14:23, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> irb(main):007:1\> end  
> from (irb):10  
> from :0  
> irb(main):011:0\>
> 
> Kind regards
> 
> robert
> 
> –  
> use.inject do |as, often| as.you\_can - without end

Wasn’t ironic Robert, I studied #yield and blocks when you mentioned  
them. It was exactly what I was looking for. Probably one of the most  
powerful assets of Ruby and I missed it.

Cheers

Aidy

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 1, 2008, 3:24pm UTC](https://www.ruby-forum.com/t/boolean-and-logging/133179/7 "2008-04-01T15:24:01Z")

</div>

2008/4/1, aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On 31 Mar, 16:56, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > The method invokes the block and uses the result to determine what to  
> > do (if or else).
> > 
> > Does that help?
> 
> Surely does. Thanks Robert, your a linguist.

Hm, not sure whether that was irony or not. 🙂

Maybe this helps:

irb(main):001:0\> def test(yes,no)  
irb(main):002:1\> if yield  
irb(main):003:2\> puts yes  
irb(main):004:2\> else  
irb(main):005:2\* puts no  
irb(main):006:2\> end  
irb(main):007:1\> end  
=\> nil  
irb(main):008:0\> test(“ok”, “not ok”) { 1 \> 2 }  
not ok  
=\> nil  
irb(main):009:0\> test(“ok”, “not ok”) { 1 \< 2 }  
ok  
=\> nil  
irb(main):010:0\> test(“ok”, “not ok”)  
LocalJumpError: no block given  
from (irb):2:in `test’  
from (irb):10  
from :0  
irb(main):011:0\>

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 2, 2008, 5:13pm UTC](https://www.ruby-forum.com/t/boolean-and-logging/133179/8 "2008-04-02T17:13:41Z")

</div>

2008/4/2, aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On 1 Apr, 14:23, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 2008/4/1,aidy[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > > Surely does. Thanks Robert, your a linguist.
> > 
> > Hm, not sure whether that was irony or not. 🙂

> Wasn’t ironic Robert, I studied #yield and blocks when you mentioned  
> them. It was exactly what I was looking for. Probably one of the most  
> powerful assets of Ruby and I missed it.

Ah! I’m glad I could help. And, yes, blocks (anonymous functions) are  
one of the best features in Ruby - totally agree.

Kind regards

robert
