[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

evaluation in boolean context

Jens Wille

10/10/2006 12:51:00 PM

hi there!

would it be possible to modify how an object gets evaluated in
boolean context? i know that by default only nil and false are
evaluated as false, all others are true. however, i'd like to change
that behaviour for a (yet unimplemented) BooleanFlag object which
behaves just like true or false (according to its current value or
"state") with the added ability of easily swapping state.

it's not that difficult to implement a class that provides the state
swapping, but how to grasp that "evaluation in boolean context" kind
of thing? is there any method getting called, so i can override it?

the idea is as follows:

b = BooleanFlag.new false # create object, initially being "false"
puts "true" if b # this is the heavy part, since b (as a
# regular object) is always "true"
b.swap! # now b's state is "true" ...
puts "true" if b # ... and this *correctly* prints "true"

any insights? additionally, hints on how to address such issues on
my own are very welcome. i'm quite new to ruby (coming from perl),
but i definitely love it - especially the metaprogramming stuff ;-)
so maybe there's a means of digging into such internals i'm not yet
aware of.

TIA
jens

--
Jens Wille, Dipl.-Bibl. (FH)
prometheus - Das verteilte digitale Bildarchiv für Forschung & Lehre
An St. Laurentius 4, 50931 Köln
Tel.: +49 (0)221 470-6668, E-Mail: jens.wille@uni-koeln.de
http://www.prometheus-bild...

10 Answers

Bira

10/10/2006 12:58:00 PM

0

On 10/10/06, jens wille <jens.wille@uni-koeln.de> wrote:
> hi there!
>
> would it be possible to modify how an object gets evaluated in
> boolean context? i know that by default only nil and false are
> evaluated as false, all others are true. however, i'd like to change
> that behaviour for a (yet unimplemented) BooleanFlag object which
> behaves just like true or false (according to its current value or
> "state") with the added ability of easily swapping state.


I'm not an expert myself, but couldn't you use a normal variable which
is assigned a value of either true or false?

b = false

puts "true" if b

b = !b

puts "true" if b

--
Bira
http://compexplicita.bl...
http://sinfoniaferida.bl...

Jens Wille

10/10/2006 1:01:00 PM

0

Bira [10/10/06 14:58]:
> I'm not an expert myself, but couldn't you use a normal variable
> which is assigned a value of either true or false?
>
> b = false
>
> puts "true" if b
>
> b = !b
yeah, but that's exactly what i want to avoid ;-)

thanks anyway!
jens

--
Jens Wille, Dipl.-Bibl. (FH)
prometheus - Das verteilte digitale Bildarchiv für Forschung & Lehre
An St. Laurentius 4, 50931 Köln
Tel.: +49 (0)221 470-6668, E-Mail: jens.wille@uni-koeln.de
http://www.prometheus-bild...

Robert Klemme

10/10/2006 1:07:00 PM

0

On 10.10.2006 14:50, jens wille wrote:
> hi there!
>
> would it be possible to modify how an object gets evaluated in
> boolean context? i know that by default only nil and false are
> evaluated as false, all others are true. however, i'd like to change
> that behaviour for a (yet unimplemented) BooleanFlag object which
> behaves just like true or false (according to its current value or
> "state") with the added ability of easily swapping state.
>
> it's not that difficult to implement a class that provides the state
> swapping, but how to grasp that "evaluation in boolean context" kind
> of thing? is there any method getting called, so i can override it?
>
> the idea is as follows:
>
> b = BooleanFlag.new false # create object, initially being "false"
> puts "true" if b # this is the heavy part, since b (as a
> # regular object) is always "true"
> b.swap! # now b's state is "true" ...
> puts "true" if b # ... and this *correctly* prints "true"

This has come up frequently in the past. The short story is that there
is no way to do exactly that because of performance reasons. All sorts
of things have been proposed including a method to_b defined in Object
as "return self" and which is automatically called in every boolean context.

> any insights? additionally, hints on how to address such issues on
> my own are very welcome. i'm quite new to ruby (coming from perl),
> but i definitely love it - especially the metaprogramming stuff ;-)
> so maybe there's a means of digging into such internals i'm not yet
> aware of.

You can still use the #to_b pattern explicitly. This is probably the
cleanest solution:

BooleanFlag = Struct.new(:flag) do
def swap!() self.flag = !flag end
alias :to_b :flag
end

>> f = BooleanFlag.new false
=> #<struct BooleanFlag flag=false>
>> f.to_b
=> false
>> f.swap!
=> true
>> f.to_b
=> true

Alternatively / additionally you can define methods on your flag class
similar to what Smalltalk does:

BooleanFlag = Struct.new(:flag) do
def swap!() self.flag = !flag end
alias :to_b :flag
def iff() yield if to_b; self end
def els() yield unless to_b; end
end

>> f=BooleanFlag.new false
=> #<struct BooleanFlag flag=false>
>> f.iff do puts "yes" end.els do puts "no" end
no
=> nil

Frankly, I never felt the need for any of these. Maybe you present your
business case and we can propose some other solution.

Kind regards

robert

Jens Wille

10/10/2006 1:32:00 PM

0

hi robert!

Robert Klemme [10/10/06 15:10]:
> This has come up frequently in the past. The short story is that
> there is no way to do exactly that because of performance
> reasons. All sorts of things have been proposed including a
> method to_b defined in Object as "return self" and which is
> automatically called in every boolean context.
i've read some of these postings, but felt they didn't add much to
what i wanted to achieve.

> You can still use the #to_b pattern explicitly. This is probably
> the cleanest solution:
yes, it probably is. however, i had kind of hoped that there would
be a nice solution that fit more in my usage example. maybe i just
have to drop that ;-) thanks for your suggestions!

> Frankly, I never felt the need for any of these. Maybe you
> present your business case and we can propose some other
> solution.
well, actually i don't have the need either. i tried to do it just
for the fun of it - to explore ruby's possibilites and play around a
bit. i mean, as long as it can be achieved in another way there
simply is no need for any more "sophisticated" solution, is it? what
kind of bugged me was that "flag = !flag" part, but obviously i just
have to stick with it...

cheers
jens

--
Jens Wille, Dipl.-Bibl. (FH)
prometheus - Das verteilte digitale Bildarchiv für Forschung & Lehre
An St. Laurentius 4, 50931 Köln
Tel.: +49 (0)221 470-6668, E-Mail: jens.wille@uni-koeln.de
http://www.prometheus-bild...

Rick DeNatale

10/10/2006 8:38:00 PM

0

On 10/10/06, Robert Klemme <shortcutter@googlemail.com> wrote:

> Alternatively / additionally you can define methods on your flag class
> similar to what Smalltalk does:

Some observations on booleans in Ruby, Smalltalk, and self:

http://talklikeaduck.denh...articles/2006/10/10/boolean-implementations-ruby-smallta...
--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Robert Klemme

10/11/2006 3:17:00 AM

0

Rick DeNatale wrote:
> On 10/10/06, Robert Klemme <shortcutter@googlemail.com> wrote:
>
>> Alternatively / additionally you can define methods on your flag class
>> similar to what Smalltalk does:
>
> Some observations on booleans in Ruby, Smalltalk, and self:
>
> http://talklikeaduck.denhaven2.com/articles/2006/10/10/boolean-implementations-ruby-smallta...

Rick, thanks for the interesting read! My gut feeling told me to better
use "similar" and not "like" or "exactly". :-)))

Kind regards

robert

SangelNet

1/16/2009 5:42:00 PM

0

On Jan 7, 9:03 pm, "Otto Moehrbach" <moehrbachoex...@bellsouth.net>
wrote:
> In this context, the 20 is the number of the column, with Column A being the
> 1st column.  Column T is the 20th column.  The code at this point is saying
> to take action only if the entry is in Column T and in one of the listed
> sheets.  HTH  Otto"SangelNet" <upslavazq...@gmail.com> wrote in message
>
> news:027a2d11-154a-4e87-a427-8366590315c2@e10g2000vbe.googlegroups.com...
> On Jan 7, 1:50 pm, "Otto Moehrbach" <moehrbachoex...@bellsouth.net>
> wrote:
>
>
>
> > This macro will do what you want. Read the code and ask questions as
> > needed. This macro has to go into the ThisWorkbook module of your
> > workbook.
> > HTH Otto
> > Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As
> > Range)
> > Dim ws As Worksheet
> > If (Sh.Name = "Sheet280" Or _
> > Sh.Name = "Sheet283" Or _
> > Sh.Name = "Sheet284" Or _
> > Sh.Name = "Sheet285" Or _
> > Sh.Name = "Sheet286" Or _
> > Sh.Name = "Sheet287" Or _
> > Sh.Name = "Sheet288") And _
> > Target.Column = 20 Then
> > If IsEmpty(Target.Value) Then Exit Sub
> > For Each ws In Sheets(Array("Sheet280", "Sheet283", "Sheet284",
> > "Sheet285", "Sheet286", "Sheet287", "Sheet288"))
> > If ws.Name <> Sh.Name Then
> > With ws
> > If Application.CountIf(.Range("T:T"), Target.Value) > 0
> > Then
> > MsgBox "Duplicate entry found in sheet " & ws.Name &
> > ".", 16, "Duplicate Found"
> > Exit Sub
> > End If
> > End With
> > End If
> > Next ws
> > End If
> > End Sub"SangelNet" <upslavazq...@gmail.com> wrote in message
>
> >news:99178f7a-2369-477c-b1aa-eb90a0fb8fb1@i24g2000prf.googlegroups.com...
>
> > > Hi guys,
>
> > > in the intent to create a macro that checks for a dupe number with in
> > > several sheets in a workbook(not all),
>
> > > sheets 280 and 283 thru 288 i want to be able to be in column T and
> > > enter a number if that number is already in column t of any of the
> > > other sheets a msgbox will pop.
>
> > > this is what i come up with.since im just strating to code its not
> > > working. what is wrong here?
>
> > > Sub dupcheck()
> > > Dim c As Range
> > > Dim ilast As Long
>
> > > ilast = cell(Rows.Count, "t").End(xlUp).Row
> > > For Each c In Workbook("sheet283:sheet288;sheet280").range("t4:T"
> > > & ilast)
> > > If Active.cell.Value = c.Range.Value Then
> > > MsgBox ("message here")
> > > End If
>
> > > thnx
>
> thnx for the reply
>
> im kinda lost in the part that says target.column = 20
>
> the 20 represents or should represent the value or the colunm?

hi there, sorry about that ..I was out for a bit.

ran the code and got an error in this line:

For Each ws In Sheets(Array("Sheet280", "Sheet283", "Sheet284",
"Sheet285", "Sheet286", "Sheet287", "Sheet288"))

could it be because its refering to the sheets in sheets. I dont know
much, just speculating.

really interested in getting it to work.

thnx

SangelNet

1/16/2009 6:08:00 PM

0

On Jan 7, 9:03 pm, "Otto Moehrbach" <moehrbachoex...@bellsouth.net>
wrote:
> In this context, the 20 is the number of the column, with Column A being the
> 1st column.  Column T is the 20th column.  The code at this point is saying
> to take action only if the entry is in Column T and in one of the listed
> sheets.  HTH  Otto"SangelNet" <upslavazq...@gmail.com> wrote in message
>
> news:027a2d11-154a-4e87-a427-8366590315c2@e10g2000vbe.googlegroups.com...
> On Jan 7, 1:50 pm, "Otto Moehrbach" <moehrbachoex...@bellsouth.net>
> wrote:
>
>
>
> > This macro will do what you want. Read the code and ask questions as
> > needed. This macro has to go into the ThisWorkbook module of your
> > workbook.
> > HTH Otto
> > Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As
> > Range)
> > Dim ws As Worksheet
> > If (Sh.Name = "Sheet280" Or _
> > Sh.Name = "Sheet283" Or _
> > Sh.Name = "Sheet284" Or _
> > Sh.Name = "Sheet285" Or _
> > Sh.Name = "Sheet286" Or _
> > Sh.Name = "Sheet287" Or _
> > Sh.Name = "Sheet288") And _
> > Target.Column = 20 Then
> > If IsEmpty(Target.Value) Then Exit Sub
> > For Each ws In Sheets(Array("Sheet280", "Sheet283", "Sheet284",
> > "Sheet285", "Sheet286", "Sheet287", "Sheet288"))
> > If ws.Name <> Sh.Name Then
> > With ws
> > If Application.CountIf(.Range("T:T"), Target.Value) > 0
> > Then
> > MsgBox "Duplicate entry found in sheet " & ws.Name &
> > ".", 16, "Duplicate Found"
> > Exit Sub
> > End If
> > End With
> > End If
> > Next ws
> > End If
> > End Sub"SangelNet" <upslavazq...@gmail.com> wrote in message
>
> >news:99178f7a-2369-477c-b1aa-eb90a0fb8fb1@i24g2000prf.googlegroups.com...
>
> > > Hi guys,
>
> > > in the intent to create a macro that checks for a dupe number with in
> > > several sheets in a workbook(not all),
>
> > > sheets 280 and 283 thru 288 i want to be able to be in column T and
> > > enter a number if that number is already in column t of any of the
> > > other sheets a msgbox will pop.
>
> > > this is what i come up with.since im just strating to code its not
> > > working. what is wrong here?
>
> > > Sub dupcheck()
> > > Dim c As Range
> > > Dim ilast As Long
>
> > > ilast = cell(Rows.Count, "t").End(xlUp).Row
> > > For Each c In Workbook("sheet283:sheet288;sheet280").range("t4:T"
> > > & ilast)
> > > If Active.cell.Value = c.Range.Value Then
> > > MsgBox ("message here")
> > > End If
>
> > > thnx
>
> thnx for the reply
>
> im kinda lost in the part that says target.column = 20
>
> the 20 represents or should represent the value or the colunm?

hi there, sorry about that ..I was out for a bit.

ran the code and got an error in this line:

For Each ws In Sheets(Array("Sheet280", "Sheet283", "Sheet284",
"Sheet285", "Sheet286", "Sheet287", "Sheet288"))

could it be because its refering to the sheets in sheets. I dont know
much, just speculating.

really interested in getting it to work.

thnx

SangelNet

1/19/2009 3:54:00 PM

0

On Jan 16, 5:53 pm, Leith Ross <Leith.Ross.3m4...@thecodecage.com>
wrote:
> Hello SangelNet,
>
> Provided the worksheet names are "Sheet280" etc., this code will take
> check column "T" of sheet for duplications of whatever the active cell
> contains.
>
> ==================================
> Sub dupcheck()
>
> Dim Dupe As Variant
> Dim ID As Variant
> Dim Wks As Worksheet
>
> For Each ID In Array(280, 283, 284, 285, 286, 287)
> Set Wks = Worksheets("Sheet" & ID)
> Dupe = WorksheetFunction.CountIf(Wks.Columns("A"),
> ActiveCell.Value)
> If Dupe > 0 Then
> MsgBox "Value has already been entered."
> End If
> Next ID
>
> End Sub
> ==================================
>
> --
> Leith Ross
>
> Sincerely,
> Leith Ross
>
> 'The Code Cage' (http://www.thecod...)
> ------------------------------------------------------------------------
> Leith Ross's Profile:http://www.thecod...forumz/member.php?userid=75
> View this thread:http://www.thecod...forumz/showthread.php?t=47606

I ran the code. placed it under "this workbook". changed the
corresponding letter for the column, but its not giving me an error
nor a result message.
played around with it for a bit and still, nothing.

SangelNet

1/19/2009 6:21:00 PM

0

On Jan 19, 11:54 am, SangelNet <upslavazq...@gmail.com> wrote:
> On Jan 16, 5:53 pm, Leith Ross <Leith.Ross.3m4...@thecodecage.com>
> wrote:
>
>
>
> > Hello SangelNet,
>
> > Provided the worksheet names are "Sheet280" etc., this code will take
> > check column "T" of sheet for duplications of whatever the active cell
> > contains.
>
> > ==================================
> > Sub dupcheck()
>
> > Dim Dupe As Variant
> > Dim ID As Variant
> > Dim Wks As Worksheet
>
> > For Each ID In Array(280, 283, 284, 285, 286, 287)
> > Set Wks = Worksheets("Sheet" & ID)
> > Dupe = WorksheetFunction.CountIf(Wks.Columns("A"),
> > ActiveCell.Value)
> > If Dupe > 0 Then
> > MsgBox "Value has already been entered."
> > End If
> > Next ID
>
> > End Sub
> > ==================================
>
> > --
> > Leith Ross
>
> > Sincerely,
> > Leith Ross
>
> > 'The Code Cage' (http://www.thecod...)
> > ------------------------------------------------------------------------
> > Leith Ross's Profile:http://www.thecod...forumz/member.php?userid=75
> > View this thread:http://www.thecod...forumz/showthread.php?t=47606
>
> I ran the code. placed it under "this workbook". changed the
> corresponding letter for the column,  but its not giving me an error
> nor a result message.
> played around with it for a bit and still, nothing.

when i run the macro im getting a :
Application Defined Or Object-defined Error