Optimization

I could use some help in optimizing a rather simple method. All it does
is randomly select a few instance variables and increase them while
decreasing another. The problem is that whether or not that particular
variable can be increased depends on if it's value is less than that of
another variable. This is what I was thinking.

[untested code]
class Foo
  def initialize(x)
    @a = 0
    @b = 0
    @c = 0
    @d = 0
    @x = x
    bar()
  end

  def bar
    values = [@a,@b,@c,@d]
    while @x > values.sort.first
      selection = values[rand(values.size)]
      if selection <= @x
        @x-=selection;selection+=1
      else
        values.delete selection
      end
    end
  end

end
[/untested code]
Any ideas?

···

--
Posted via http://www.ruby-forum.com/.