Sha256: a2516857d5fccc5500ab649f50144349e7ac854d775eca30b4e221629a1cc2a4

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

# Author::    Nicolas Pouillard  <ertai@lrde.epita.fr>.
# Copyright:: Copyright (c) 2004, 2005 TTK team. All rights reserved.
# License::   LGPL
# $Id: filters.rb 567 2005-04-13 08:00:06Z polrop $


require 'io/ext'
require 'delegate'

class IO
  class InputFilter < DelegateClass(IO)
    def initialize(input, &block)
      @input = input
      @block = block
    end
    def each(&block)
      @input.each { |line| block[@block[line]] }
    end
    def each_byte(&block)
      @input.each_byte { |line| block[@block[line]] }
    end
    def each_line(&block)
      @input.each_line { |line| block[@block[line]] }
    end
    def getc
      @block[@input.getc]
    end
    def gets
      @block[@input.getc]
    end
    def read
      @block[@input.read]
    end
    def readlines
      @input.readlines.map(&@block)
    end
    alias readchar getc
    alias readline gets
  end

  class OutputFilter < DelegateClass(IO)
    def initialize(output, &block)
      @output = output
      @block = block
    end
    def <<(o)
      @output.write @block[o.to_s]
      self
    end
    def print(*args)
      @output.print args.map { |x| @block[x.to_s] }
    end
    def printf(fmt, *args)
      @output.printf fmt, args.map { |x| @block[x] }
    end
    def putc(c)
      @output.putc @block[c]
    end
    def puts(*args)
      if args.size == 0
	@output.print @block["\n"]
      else
	@output.print args.map { |x| @block[x.to_s] }
      end
    end
    def write(o)
      @output.write @block[o.to_s]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ttk-0.1.576 lib/ttk/Tools/io/filters.rb
ttk-0.1.580 lib/ttk/Tools/io/filters.rb
ttk-0.1.579 lib/ttk/Tools/io/filters.rb