From: dblack@... Date: 2002-09-30T09:25:14+09:00 Subject: Re: thoughts on typelessness Hi -- On Mon, 30 Sep 2002, Pit Capitain wrote: > On 29 Sep 2002, at 12:09, dblack@candle.superlink.net wrote: > > > def sort_key > > @sort_key - 1 > > rescue NameError > > @sort_key > > end > > When you announced dbdbd I quickly browsed through the code. I > had the impression that I understood your well written program - > besides this very example. I really tried to understand what's going > on, but I just couldn't get it. Whoops :-) > def sort_key > if @sort_key.is_a? Integer > @sort_key - 1 > else > @sort_key > end > end > > I'm sure it would be very interesting to try to program without #type, > #is_a? etc and I'm sure I will try to do so in the future, but for me > this wasn't a good example. If you think so, too, maybe you could > find an other implementation. I'd be very interested in what you'd > come up with. One direction one can go in -- probably heavy overkill for this example, but anyway -- is extending the capacity of objects. module MinusStub def -(val) self end end class Thing def sort_key @sort_key - 1 end def sort_key=(val) @sort_key = val @sort_key.extend(MinusStub) unless @sort_key.respond_to?(:-) end end if __FILE__ == $0 t = Thing.new t.sort_key = 5 p t.sort_key # 4 p t.sort_key * 10 # 40 t.sort_key = "Hello" p t.sort_key # "Hello" end (I know there are some issues about this approach to maintaining a simultaneously 0- and 1-originating array... but I won't go into all of that here :-) I actually really like Massimiliano's #can? idea: @sort_key.extend(MinusStub) unless @sort_key.can?(:-) looks nice to me, and slightly more abstracted (in a good way, I think) than respond_to? David -- David Alan Black | Register for RubyConf 2002! home: dblack@candle.superlink.net | November 1-3 work: blackdav@shu.edu | Seattle, WA, USA Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com