On Thu, 22 Mar 2001, Hugh Sasse Staff Elec Eng wrote:
> I've run up against something I thought I knew how to solve, but...
> class IO
>     def outputbit(bit)
>         @c = (@c << 1) | bit
>         @bitcount += 1
> 	if ((@bitcount % 8) == 0)
> 	    putc(c)
>             c = 0
>         end
>     end
> end

> OK, horribly inefficient, but still.  I have extended IO.  Now, how
> do I get IO to initialize @c and @bitcount properly?

I'd recommend against doing such tricks of renaming methods. I'd keep
that for cases where there is no other solution.

class IO
  def outputbit(bit)
    @c ||= 0
    @bitcount ||= 0
    @c = (@c << 1) | bit
    @bitcount += 1
    if @bitcount == 8 then
      putc(@c)
      @c = 0
      @bitcount = 0
    end
  end
end

or

class IO
  def outputbit(bit)
    @c = ((@c||1) << 1) | bit
    if @c[8]==1 then
      putc(@c)
      @c = 1
    end
  end
end

matju