evaluation in boolean context

J

jens wille

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: (e-mail address removed)
http://www.prometheus-bildarchiv.de/
 
B

Bira

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
 
J

jens wille

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: (e-mail address removed)
http://www.prometheus-bildarchiv.de/
 
R

Robert Klemme

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
=> 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
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
 
J

jens wille

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: (e-mail address removed)
http://www.prometheus-bildarchiv.de/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top