Sha256: d7f91176ad1cd1f128b47f3c9dc02f75b6dcf8288043125454e7296f34f40a64

Contents?: true

Size: 1.2 KB

Versions: 12

Compression:

Stored size: 1.2 KB

Contents

class IO

  @__fs__ = node_require :fs
  `var __fs__ = #{@__fs__}`

  attr_reader :eof
  attr_reader :lineno

  def initialize
    @eof = false
    @lineno = 0
  end

  def read
    if @eof
      ''
    else
      res = `__fs__.readFileSync(#{@path}).toString()`
      @eof = true
      @lineno = res.size
      res
    end
  end

  def each_line(separator = $/, &block)
    if @eof
      return block_given? ? self : [].to_enum
    end

    if block_given?
      lines = File.read(@path)
      %x{
        self.eof = false;
        self.lineno = 0;
        var chomped  = #{lines.chomp},
            trailing = lines.length != chomped.length,
            splitted = chomped.split(separator);
        for (var i = 0, length = splitted.length; i < length; i++) {
          self.lineno += 1;
          if (i < length - 1 || trailing) {
            #{yield `splitted[i] + separator`};
          }
          else {
            #{yield `splitted[i]`};
          }
        }
        self.eof = true;
      }
      self
    else
      read.each_line
    end
  end
end

STDOUT.write_proc = -> (string) {`process.stdout.write(string)`}
STDERR.write_proc = -> (string) {`process.stderr.write(string)`}

STDOUT.tty = true
STDERR.tty = true

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
opal-0.10.6 stdlib/nodejs/io.rb
opal-0.10.6.beta stdlib/nodejs/io.rb
opal-0.10.5 stdlib/nodejs/io.rb
opal-0.10.4 stdlib/nodejs/io.rb
opal-0.10.3 stdlib/nodejs/io.rb
opal-0.10.2 stdlib/nodejs/io.rb
opal-0.10.1 stdlib/nodejs/io.rb
opal-0.10.0 stdlib/nodejs/io.rb
opal-0.10.0.rc2 stdlib/nodejs/io.rb
opal-0.10.0.rc1 stdlib/nodejs/io.rb
opal-0.10.0.beta5 stdlib/nodejs/io.rb
opal-0.10.0.beta4 stdlib/nodejs/io.rb