Class: Ru::Stream
- Inherits:
-
Object
show all
- Defined in:
- lib/ru/stream.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(stream) ⇒ Stream
Returns a new instance of Stream.
48
49
50
51
|
# File 'lib/ru/stream.rb', line 48
def initialize(stream)
raise ArgumentError unless stream.kind_of? Enumerator::Lazy
@stream = stream
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/ru/stream.rb', line 115
def method_missing(method, *args, &block)
result = @stream.send(method, *args, &block)
if result.kind_of? Enumerator::Lazy
self.class.new(result)
elsif result.kind_of? ::Array
Ru::Array.new(result)
else
result
end
end
|
Instance Attribute Details
#stream ⇒ Object
Returns the value of attribute stream.
45
46
47
|
# File 'lib/ru/stream.rb', line 45
def stream
@stream
end
|
Instance Method Details
#==(other) ⇒ Object
109
110
111
|
# File 'lib/ru/stream.rb', line 109
def ==(other)
other.is_a?(self.class) && self.stream == other.stream
end
|
#each_line ⇒ Object
53
54
55
|
# File 'lib/ru/stream.rb', line 53
def each_line
Ru::Iterator.new(self)
end
|
#files ⇒ Object
57
58
59
|
# File 'lib/ru/stream.rb', line 57
def files
self.class.new @stream.map { |line| Ru::File.new(line) }
end
|
61
62
63
|
# File 'lib/ru/stream.rb', line 61
def format(format='l')
self.class.new @stream.map { |item| item.format(format) }
end
|
#grep(pattern) ⇒ Object
65
66
67
68
69
70
|
# File 'lib/ru/stream.rb', line 65
def grep(pattern)
if pattern.kind_of? String
pattern = Regexp.new(pattern)
end
self.class.new @stream.select { |item| item.to_s =~ pattern }
end
|
#map(method = nil, *args, &block) ⇒ Object
72
73
74
75
76
77
78
79
80
|
# File 'lib/ru/stream.rb', line 72
def map(method=nil, *args, &block)
if method.nil? && !block_given?
each_line
elsif method.nil?
self.class.new @stream.map(&block)
else
self.class.new @stream.map { |item| item.send(method, *args) }
end
end
|
#select(*args, &block) ⇒ Object
82
83
84
|
# File 'lib/ru/stream.rb', line 82
def select(*args, &block)
self.class.new @stream.select(*args, &block)
end
|
#to_a ⇒ Object
Also known as:
to_ary
86
87
88
|
# File 'lib/ru/stream.rb', line 86
def to_a
Ru::Array.new(@stream.to_a)
end
|
#to_s ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/ru/stream.rb', line 92
def to_s
result = ''
result.concat @stream.next.to_s
loop do
item = @stream.next.to_s
result.concat "\n".freeze
result.concat item
end
result
rescue StopIteration
result
end
|
#to_self ⇒ Object
105
106
107
|
# File 'lib/ru/stream.rb', line 105
def to_self
self
end
|