From: Marnen Laibow-Koser Date: 2009-11-17T08:55:43+09:00 Subject: Re: Newbie question: Defining a numeric type Seebs wrote: > On 2009-11-16, Marnen Laibow-Koser wrote: >> a = Stat.new(5) >> b = Stat.new(5) >> >> Should a == b return true or false? > >> If false, then this is probably not a value object in the conventional >> sense. > >> If true, then it probably is. > > Hmm. > > I would think it should return true. So would I. > >> Poor analogy. I think your paycheck is like your character's entire >> stat block, whereas the individual Stat objects seem to be analogous to >> the dollar amounts on each like of the paycheck -- and those are >> certainly value objects. > > An individual stat might have: > base value 10 > modifier (racial, no duration) +3 > modifier (training, no duration) +2 > modifier (drunk, duration 15 turns) -1 > > That's "a 14". It is *equal to* another stat which has a base value of > 14 > and no modifiers. But it's not the same *thing* as that other stat. Of course it isn't. Here's how I see this working (for simplicity, I'll just assume one modifier): a = Stat.new 10, 0 b = Stat.new 10, 2 c = Stat.new 12, -2 d = Stat.new 12, -2 c == d # true a == b # false a == c # false a.to_i == c.to_i # true Does that all seem right? > >> And I think you're probably wrong. >> >> john = Character.new >> john.strength = Stat.new(15) >> mary = Character.new >> mary.strength = john.strength >> mary.strength += 1 >> >> Now, what should john.strength be? 15 or 16? > > 15. But you don't copy stats -- you use their values. Right! In other words, they're identified by their value, not by the individual object reference. Ergo, *they are value objects*, by definition. > So what really > happens is that mary defines strength= such that it applies modifiers > to mary.strength. Or simply creates a new immutable value object, which will save some headache. > > In fact, in "john.strength = Stat.new(15)", what really happens is that > john does the same thing -- adjust the already-existing stat so that it > will > have an effective value of 15. No. If you call a constructor, you might as well use the object. :) > > Hmm. > > So, thinking about it, it seems like what I'm getting to is that really, > the "Stat" class is largely internal to characters. It's a way to > express > something that, in general, no one else should use. (Not quite true; > you can query the modifiers that currently apply to a stat, for > instance.) So you *do* want to do something like john.strength.modifiers[:racial], right? If so, then the Stat class is not internal. Personally, I think that's fine -- a stat is not jet a simple number, and so you might as well represent it with an appropriate value object. > For the purposes of nearly any possible interaction, john.strength is > just the value 15. The easiest way to achieve that is with conversion and/or coercion. > > You're asking some really good questions here, thanks. > > -s You're welcome! These are questions I struggle with as well -- I like to learn by teaching. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen@marnen.org -- Posted via http://www.ruby-forum.com/.