From: Seebs Date: 2009-11-18T01:10:10+09:00 Subject: Re: Newbie question: Defining a numeric type On 2009-11-17, Brian Candler wrote: > 1. Whilst you want the individual stats to behave as integers, you > probably don't want the entire Character to behave as an integer - the > Character is likely to have many other attributes, such as 'name' in the > above example. So, whenever you deal with individual stats, you are > always going to do c.str or c.str=val. This is a convenient place to > masquerade the Stat. Actually, yes. > 2. When you set the strength of a character, you don't want to replace > the entire Stat object, you want to update its state so it can retain > history. So when you write > c.str = x > then really you don't want to replace the strength Stat object inside c; > you are sending a message to it to update its state. If you use the code > I've shown above, then it avoids you having to write > c.str.val = x > which is what you'd have to do if c.str returned the Stat object itself. Actually, not exactly. The thing is, when you write "c.str = x", you aren't sending a message to c.str -- you're sending "str=" to c. So I had handled this by wrapping "str=" differently, but having "str" just yield the stat object. Which meant I had to do extra work to make it just look like an integer the rest of the time, admittedly. > And if you really want to, you can define to_int and method_missing so > that the entire Character resolves to its level value. But I think this > is more likely to be confusing rather than helpful. If you ever want to > do arithmetic on levels, I think it would be clearer to see "ogre.level > - elf.level" rather than "ogre - elf", because ogres have more > attributes than just their level. Oh, I would have no intent ever of trying to make characters comparable. If I needed to sort a list of critters, that'd be time for a sort_by. -s -- Copyright 2009, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!