From: Robert Klemme Date: 2004-05-23T19:48:44+09:00 Subject: Re: Zero is true ... whoda thunk? "Dan Janowski" schrieb im Newsbeitrag news:AE82CDF1-AAE8-11D8-AC4D-00039357B54C@3skel.com... > I found this problem thorny when trying to export true/false to a > database that does not support booleans (only int). So I wrote a class > that magically does the conversion coming and going. Other than that, I > find nil a very useful value and nice to be separate from numbers. That inspired me to this piece of code: CONVERSIONS = {} def convert(obj) cv = find_converter obj cv ? cv.call( obj ) : obj end def find_converter(obj) obj.class.ancestors.each do |cl| x = CONVERSIONS[cl] return x if x end nil end CONVERSIONS[1.class] = proc {|x| x != 0} convert 1023 You could even extend this to handle different target contexts (sometimes you want int -> bool, sometimes int -> float): def convert(obj, target=nil) cv = find_converter obj, target cv ? cv.call( obj ) : obj end def find_converter(obj, target) if target obj.class.ancestors.each do |cl| x = CONVERSIONS[ [cl, target] ] return x if x end else obj.class.ancestors.each do |cl| x = CONVERSIONS[cl] return x if x end end nil end .... Regards robert