indented_io 0.7.3 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/TODO +8 -2
- data/lib/indented_io/indented_io.rb +21 -28
- data/lib/indented_io/indented_io_interface.rb +2 -2
- data/lib/indented_io/version.rb +1 -1
- data/scripts/perf.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '097c526b0eeabff0b3be7d1483a6bac19232b803ddb630457c56ca4e7d180cea'
|
4
|
+
data.tar.gz: 0a6dc9f1bdf3dbc325ea52d06298b67dd7be389297baadc85d773e4f23edd329
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 516212abf50023dd89352d55f2bf2745e367cf3e0646d3f0dc1c5d6a8193cf0fdc8053beec7259e978ad7bd94791cd6e8476c9ba101e2a066b93b311d9589b84
|
7
|
+
data.tar.gz: 4b8ead522fa1830c8c31888cded99ae00679737dd6f3560ebe9214190b27c38b2af2ff224437cb8c7be5e2de7fe338ca06525d5d7d980eb088f5dbc996380e6b
|
data/README.md
CHANGED
@@ -142,14 +142,14 @@ In case of errors an `IndentedIO::Error` exception is raised
|
|
142
142
|
|
143
143
|
You can add support for your own IO objects by including
|
144
144
|
`IndentedIO::IndentedIOInterface` in your class. All that is required is that
|
145
|
-
the class define a `#
|
146
|
-
|
145
|
+
the class define a `#write` method with the same semantics as `IO#write`
|
146
|
+
(convert arguments to strings and then write them)
|
147
147
|
|
148
148
|
```ruby
|
149
149
|
require 'indented_io'
|
150
150
|
class MyIO
|
151
151
|
include IndentedIO::IndentedIOInterface
|
152
|
-
def
|
152
|
+
def writte(*args) ... end
|
153
153
|
end
|
154
154
|
|
155
155
|
my_io = MyIO.new
|
@@ -174,7 +174,7 @@ check performance). It would be much faster if the inner loop was implemented
|
|
174
174
|
in C. However, we're talking micro-seconds here: Printing without using
|
175
175
|
IndentedIO range from around 0.25ms to 1ms while using IndentedIO slows it down
|
176
176
|
to between 4 and 12 microseconds, so IndentedIO won't cause a noticeable slow
|
177
|
-
down of your application
|
177
|
+
down of your application unless you do a lot of output
|
178
178
|
|
179
179
|
## Installation
|
180
180
|
|
data/TODO
CHANGED
@@ -2,14 +2,20 @@
|
|
2
2
|
TODO
|
3
3
|
o Check if IO object is writable - no good solution ?
|
4
4
|
o better name for @this_indent ? 'string' ?
|
5
|
-
o Change dependency in IndentedIOInterface from #print to #write and bump
|
6
|
-
version to 0.8 as this is the last user-visible change to be made
|
7
5
|
o Reimplement core loop in C
|
8
6
|
o Create BaseIndentedIO to hold bol status and remove it from IndentedIO
|
9
7
|
o Transmogrify instances variables and protected methods in IndentedIO to
|
10
8
|
avoid collision with names from the underlying device
|
11
9
|
o Explain name collision issues in the docs
|
10
|
+
o Remove IndentedIO#dump
|
12
11
|
|
12
|
+
+ Change dependency in IndentedIOInterface from #print to #write and bump
|
13
|
+
version to 0.8 as this is the last user-visible change to be made
|
14
|
+
+ Oops. #print reimplemention doesn't do this:
|
15
|
+
If the output field separator ($,) is not nil, it is inserted between
|
16
|
+
objects. If the output record separator ($\) is not nil, it is appended to
|
17
|
+
the output
|
18
|
+
+ Tests for #write
|
13
19
|
+ explain bol
|
14
20
|
+ Allow a symbolic :string argument
|
15
21
|
+ #printf !
|
@@ -20,48 +20,54 @@ module IndentedIO
|
|
20
20
|
interface_indent(levels, string, bol: bol, &block)
|
21
21
|
end
|
22
22
|
|
23
|
-
# Indent and print args to the underlying device. #
|
24
|
-
# as
|
25
|
-
def
|
26
|
-
|
27
|
-
@device.print @combined_indent
|
28
|
-
self.bol = false
|
29
|
-
end
|
23
|
+
# Indent and print args to the underlying device. #write has the same semantic
|
24
|
+
# as IO#write
|
25
|
+
def write(*args)
|
26
|
+
n_bytes = 0
|
30
27
|
args.join.each_char { |c|
|
31
28
|
if c == "\n"
|
32
29
|
self.bol = true
|
33
30
|
elsif bol
|
34
|
-
@device.
|
31
|
+
n_bytes += @device.write(@combined_indent)
|
35
32
|
self.bol = false
|
36
33
|
end
|
37
|
-
@device.
|
34
|
+
n_bytes += @device.write(c)
|
38
35
|
}
|
36
|
+
n_bytes
|
37
|
+
end
|
38
|
+
|
39
|
+
# Indent and print args to the underlying device. #print has the same semantic
|
40
|
+
# as Kernel#print
|
41
|
+
def print(*args)
|
42
|
+
return nil if args.empty?
|
43
|
+
write(args.join($, || ''))
|
44
|
+
write($\) if $\
|
39
45
|
nil
|
40
46
|
end
|
41
47
|
|
42
48
|
# Indent and print args to the underlying device. #printf has the same semantic
|
43
49
|
# as Kernel#printf
|
44
50
|
def printf(format, *args)
|
45
|
-
|
51
|
+
write format % args
|
46
52
|
end
|
47
53
|
|
48
54
|
# Indent and print args to the underlying device. #puts has the same semantic
|
49
55
|
# as Kernel#puts
|
50
56
|
def puts(*args)
|
51
|
-
args.
|
57
|
+
write args.join("\n"), "\n"
|
52
58
|
nil
|
53
59
|
end
|
54
60
|
|
55
61
|
# Indent and print args to the underlying device. #p has the same semantic
|
56
62
|
# as Kernel#p. Please note that #p is usually not defined on other classes
|
57
|
-
#
|
63
|
+
# than Kernel but can be used on any IndentedIO object
|
58
64
|
def p(*args)
|
59
65
|
if bol
|
60
|
-
args.each { |arg|
|
66
|
+
args.each { |arg| write(arg.inspect, "\n") }
|
61
67
|
else
|
62
|
-
@device.
|
68
|
+
@device.write(args.first.inspect, "\n")
|
63
69
|
bol = true
|
64
|
-
args[1..-1].each { |arg|
|
70
|
+
args[1..-1].each { |arg| write(arg.inspect, "\n") }
|
65
71
|
end
|
66
72
|
args.size == 1 ? args.first : args
|
67
73
|
end
|
@@ -146,18 +152,5 @@ module IndentedIO
|
|
146
152
|
self.bol = (bol.nil? ? true : bol)
|
147
153
|
end
|
148
154
|
end
|
149
|
-
|
150
|
-
public
|
151
|
-
# @!visibility private
|
152
|
-
def dump
|
153
|
-
$stderr.puts "#{self.class} [#{self.object_id}]"
|
154
|
-
$stderr.puts " device: #{device.class} [#{device.object_id}]"
|
155
|
-
$stderr.puts " base : #{base.class} [#{base.object_id}]"
|
156
|
-
$stderr.puts " parent: #{parent.class} [#{parent.object_id}]"
|
157
|
-
$stderr.puts " levels: #{levels}"
|
158
|
-
$stderr.puts " this_indent: #{this_indent.inspect}"
|
159
|
-
$stderr.puts " combined_indent: #{combined_indent.inspect}"
|
160
|
-
$stderr.puts " bol: #{bol}"
|
161
|
-
end
|
162
155
|
end
|
163
156
|
end
|
@@ -1,12 +1,12 @@
|
|
1
1
|
module IndentedIO
|
2
2
|
# IndentedIO interface that provides the #indent method. It is used by IO,
|
3
3
|
# StringIO, and IndentedIO but can be included in any class that define a
|
4
|
-
# #
|
4
|
+
# #write method like this:
|
5
5
|
#
|
6
6
|
# require 'indented_io'
|
7
7
|
# class MyIO
|
8
8
|
# include IndentedIO::IndentedIOInterface
|
9
|
-
# def
|
9
|
+
# def write(*args) ... end
|
10
10
|
# end
|
11
11
|
#
|
12
12
|
# my_io = MyIO.new
|
data/lib/indented_io/version.rb
CHANGED
data/scripts/perf.rb
CHANGED