Class: IO

Inherits:
Object show all
Defined in:
opal/stdlib/nodejs/io.rb

Direct Known Subclasses

File, StringIO

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIO

Returns a new instance of IO



9
10
11
12
# File 'opal/stdlib/nodejs/io.rb', line 9

def initialize
  @eof = false
  @lineno = 0
end

Instance Attribute Details

#eofObject (readonly)

Returns the value of attribute eof



6
7
8
# File 'opal/stdlib/nodejs/io.rb', line 6

def eof
  @eof
end

#linenoObject (readonly)

Returns the value of attribute lineno



7
8
9
# File 'opal/stdlib/nodejs/io.rb', line 7

def lineno
  @lineno
end

Instance Method Details

#each_line(separator = $/, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'opal/stdlib/nodejs/io.rb', line 25

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

#readObject



14
15
16
17
18
19
20
21
22
23
# File 'opal/stdlib/nodejs/io.rb', line 14

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