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/
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/