Seebs wrote:
> This turns out not to quite be the case, in experiments.  If I do that,
> it works most of the time, but as an example:
>   john.str + john.dex + john.con
> doesn't work, because it can't figure out that ANY of them should be
> integers.

ISTM that you are over-complicating. str, dex and con are individual 
attributes of the character and can be just Fixnums.

If you want the whole combined set of attributes to act as an integer 
with a single value then that's straightforward to arrange.

class Stats
  attr_accessor :str, :dex, :con
  def initialize(str, dex, con)
    @str, @dex, @con = str, dex, con
  end
  def to_int
    str + dex + con
  end
  def to_s
    to_int.to_s
  end
  def method_missing(*args)
    to_int.send(*args)
  end
end

ogre = Stats.new(16,3,2)
elf = Stats.new(5,15,3)
puts ogre < elf                          # => true
puts elf - ogre                          # => 2
puts "Argh!" if ogre + rand(6) < elf     # => sometimes

This is the solution I posted before - what's the problem with it?

You said you wanted to remember things like the highest str and be able 
to restore it. So just include that state too.

class Stats
  attr_accessor :str, :dex, :con
  def initialize(str, dex, con)
    @str, @dex, @con = str, dex, con
    @max_str, @max_dex, @max_con = str, dex, con
  end

  def str=(x)
    @str=x
    @max_str=x if x > @max_str
  end

  def restore_strength
    @str = @max_str
  end

  def to_int
    str + dex + con
  end

  def to_s
    to_int.to_s
  end

  def method_missing(*args)
    to_int.send(*args)
  end
end

ogre = Stats.new(16,3,2)
puts ogre                    # => 21
ogre.str = 4
puts ogre                    # => 9
ogre.restore_strength
puts ogre                    # => 21

Sure, there's some duplication involved if you repeat this for 
individual stats. Is that a problem? Use a bit of metaprogramming to 
save the typing.

Or, your Stats object could include a Hash with the individual 
attributes, which would be extensible.

ogre.get(:str)       # or ogre[:str]
ogre.set(:str, 12)   # or ogre[:str] = 12
ogre.max(:str)
ogre.restore(:str)
-- 
Posted via http://www.ruby-forum.com/.