In article <20030718193620.GA23206 / student.ei.uni-stuttgart.de>,
Mauricio FernŠŌdez  <batsman.geo / yahoo.com> wrote:
>On Sat, Jul 19, 2003 at 04:05:39AM +0900, Phil Tomson wrote:
>> I needed to test a class which had a certain number of 'binary' inputs
>(ie. each input 
>> takes either 0 or 1 (or false or true) ).  In order to do this I
>needed to try out every 
>> possible combination of inputs - essentially a binary count, like:
>> 
>> 0,0,0,0
>> 0,0,0,1
>> ....
>> 1,1,1,0
>> 1,1,1,1
>> 
>> I figured I could just increment an integer and, then convert it to
>binary, but:
>>   1) there doesn't seem to be a builtin to_bin method on FixNum.
>>   2) I'd still have to deal with indexing, etc.
>>   (OK, it wouldn't be that hard to implement #1 and #2 is not that
>hard either, but I 
>> decided to go a different route for fun ;-)
>
>>> 8.times {|i| 4.times {|j| print i[3-j]}; puts }
>0000
>0001
>0010
>0011
>0100
>0101
>0110
>0111
>=> 8
>>> class Fixnum; def to_bin(bits=31); a = []; bits.times{|i| a.unshift
>self[i]}; a end end
>=> nil
>>> 10.times {|i| p i.to_bin(4)}
>[0, 0, 0, 0]
>[0, 0, 0, 1]
>[0, 0, 1, 0]
>[0, 0, 1, 1]
>[0, 1, 0, 0]
>[0, 1, 0, 1]
>[0, 1, 1, 0]
>[0, 1, 1, 1]
>[1, 0, 0, 0]
>[1, 0, 0, 1]
>

Hmmm... that was a lot easier I say sheepishly :)

But it doesn't do that 'dog' 'cat' thing ;-)

Phil