From: Matt Margolis Date: 2007-10-10T02:41:43+09:00 Subject: Re: Make a class evaluate to false in boolean expressions Robert Klemme wrote: > On 09.10.2007 18:12, Logan Capaldo wrote: >> On 10/9/07, Matt Margolis wrote: >>> Logan Capaldo wrote: >>>> On 10/9/07, Matt Margolis wrote: >>>>> Is achieving this behavior as simple as redefining some operator >>>>> methods >>>>> like == or is this sort of behavior not possible in Ruby? >>>>> >>>>> >>>> The latter. >>>> >> >> Quoting myself. In other words, no you can't do it. > > An alternative approach might be to use 'case' expressions instead of > 'if' and implement #=== properly in that class. > > Matt, what are you implementing? Why is this functionality crucial? > > Kind regards > > robert > I can't really get into specifics but basically I am doing something like the following module PrimitiveExtensions attr_accessor :name def to_extended_primitive(name) @name = name return self end I then mix this in to String, Fixnum, Float etc... MyClass contains def my_val=(obj) obj.to_extended_primitive("MyVal") end def other_val=(obj) obj.to_extended_primitive("OtherVal") end The purpose of all of this is to unify an interface so that I can store primitive values along with a name without having to go through a container class to get at the value. a = MyClass.new.my_val = 7 and then later be able to go back and do something like a.name and get back "MyVal" This approach works for all the base types except for true and false since they are Singletons so I can't store a @name on them.