From: "Simon Kröger" Date: 2005-12-02T08:03:28+09:00 Subject: Re: Optimizing for Newbies Simon Strandgaard wrote: > On 12/1/05, Matthew Feadler wrote: > >>Since I've gotten the go-ahead from Christer, here's a little newbie app >>I just wrote and am looking for advice on how to better write: > > [snip] > > > def rollAttributePair > rolls = Array.new > rolls.push(Die.new(10).roll) > rolls.push(Die.new(10).roll) > if (rolls[0] == 0) or (rolls[1] == 0) > total = 0 > return total > else > total = (rolls[0] - rolls[1]) > return total > end > end > > > maybe write it like this (untested) > > def roll_attribute_pair > r1 = Die.new(10).roll > r2 = Die.new(10).roll > return 0 if (r1 == 0) or (r2 == 0) > (r1 - r2) > end > > > -- > Simon Strandgaard or if you like your Array: def rollAttributePair rolls = Array.new(2){Die.new(10).roll} return 0 if rolls.any?{|r|r.zero?} rolls[0] - rolls[1] end you may also look at 'Struct' for the charAttributes (may just be better suited than a hash) cheers Simon