string-builder 2.1.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 968a9ed0e66b05ba5ebf1097c4eb93cfc261a4cb4d5a7834716d237bc828c80d
4
- data.tar.gz: 8ce225e70fc36e5c6fd987dcc8703273b714e8d33c4911b870b7a677cd27a829
3
+ metadata.gz: e9be05a5fa8302af3f75659e915cd1ed84f8c87ff6ff5ec177d1dde71910e66a
4
+ data.tar.gz: 6c3840f7d0ae83a84ad66439ea370b3a25f36dfaee0512c6aeaebb549f0d3253
5
5
  SHA512:
6
- metadata.gz: 43aa802365254956bf79c3fc0e6ee84e542a0c55ff456c78b867ac17f51e9030fe1ffa14f3bcbbd5b3bc723219c6f0fde97ad12842550e67a7241c865bcf2231
7
- data.tar.gz: 95a2668b3bc77a84a5b24c4f8cf446d3b9d3f8a58ceb958ac1604776c1547b58eebfa3eec4347b9a7928f7b348a81ecb9c3fdb7c17e2601625756fbcb2ed9144
6
+ metadata.gz: 8a7d470f28efeb3c0fff8539d908a105f406a8f6b2eeba496c5be6bdd61356cee33a6b96dcac7e663ee2d781c9d4ba991b28027c980e1b03fb44c9123a917493
7
+ data.tar.gz: 2af21fb239aef87a7ca89f06c7ee4b020f930f5a9c75aef42dc08c0b948f5ad9ff49897264608cc2b385b73062e74e604b2fad82ba9d4c7b2f2a1a07ccaeb9d6
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  # String::Builder
6
6
 
7
- Modified and extended port of the [String::Builder](https://crystal-lang.org/api/0.20.3/String/Builder.html#build%28capacity%3AInt%3D64%2C%26block%29%3AString-class-method) IO-style initializer for the `String` class of the Crystal programming language.
7
+ Modified and extended port of the [String::Builder](https://crystal-lang.org/api/0.20.3/String/Builder.html#build%28capacity%3AInt%3D64%2C%26block%29%3AString-class-method) IO-style initializer for the `String` class of the Crystal programming language in the form of a Ruby gem refinement module.
8
8
 
9
9
  ## Methods
10
10
 
@@ -67,6 +67,16 @@ If no block is given, then the object converted to a string (with `to_s`) is ret
67
67
 
68
68
  ##### Examples
69
69
 
70
+ ```ruby
71
+ foobar = String.build do |s|
72
+ s << 'fii'
73
+ s.gsub!('ii','oo')
74
+ s << 'bar'
75
+ end
76
+
77
+ foobar #=> "foobar"
78
+ ```
79
+
70
80
  ```ruby
71
81
  foobar = String.build 'foo' do |s|
72
82
  s << 'bop'
@@ -97,6 +107,20 @@ foobar #=> "foobar"
97
107
  foo #=> "foo"
98
108
  ```
99
109
 
110
+ #### `String.[]`
111
+
112
+ Takes arbitrarily many objects (with splat), converts them to strings and contatenates them (takes the product with the empty string).
113
+
114
+ ##### Examples
115
+
116
+ ```ruby
117
+ String[] #=> ""
118
+ String[3] #=> "3"
119
+ String['Hello','World','!'] #=> "HelloWorld!"
120
+ String[{k: 'v'}, 3, %i[a b c]] #=> "{:k=>\"v\"}3[:a, :b, :c]"
121
+ String[*['a', 2, :z]] #=> "a2z"
122
+ ```
123
+
100
124
  ## Detailed example
101
125
 
102
126
  This example shows how to make a simple logger by constructing log messages with `String::Builder`.
@@ -200,54 +224,6 @@ end
200
224
  # CANNOT use String::Builder methods in the global scope
201
225
  ```
202
226
 
203
- ## Limitations of string building in Ruby and Crystal
204
-
205
- The [String::Builder](https://crystal-lang.org/api/0.20.3/String/Builder.html) class of the Crystal programming language provides an initializer method for the `String` class called `build` which is essentially an optimized version of Ruby's `StringIO`.
206
-
207
- Ruby's `StringIO` and Crystal's `String::Builder` are great because it essentially turns strings into IO objects, allowing you to pass a block into the constructor (yielding `self`) which leads to nice chaining such as:
208
-
209
- ```ruby
210
- # Ruby - StringIO
211
- test = StringIO.open do |s|
212
- s << 'Hello '
213
- s << 'World!'
214
- s.string
215
- end
216
- #=> "Hello World!"
217
- ```
218
-
219
- Note the necessary `StringIO#string` method call. Since we are yielding a `StringIO` object, we must convert it to a `String` at the end. This is a bit cleaner in Crystal:
220
-
221
- ```ruby
222
- # Crystal - String::Builder
223
- test = String.build do |s|
224
- s << "Hello "
225
- s << "World!"
226
- end
227
- #=> "Hello World!"
228
- ```
229
-
230
- ---
231
-
232
- However, since **neither** of these two implementations yield a `String` object, you can't use `String` methods to mutate the object:
233
-
234
- ```ruby
235
- # Ruby - StringIO
236
- test = StringIO.open do |s|
237
- s << 'Hello '
238
- s << 'World!'
239
- s.upcase!
240
- s.string
241
- end
242
- #=> ... undefined method `upcase!' for #<StringIO:0x00007fe0bc09d810> (NoMethodError)
243
- ```
227
+ ## Further information
244
228
 
245
- ```ruby
246
- # Crystal - String::Builder
247
- test = String.build do |s|
248
- s << "Hello "
249
- s << "World!"
250
- s.gsub!("!", "?")
251
- end
252
- #=> ... undefined method 'gsub!' for String::Builder
253
- ```
229
+ For more information about string-building in Ruby and Crystal, [read this blog post](https://www.eonuonga.com/posts/2018/06/07/limitations-of-string-building).
@@ -1,6 +1,8 @@
1
1
  module String::Builder
2
2
  refine String.singleton_class do
3
3
 
4
+ def [](*args) args.map(&:to_s)*self.new end
5
+
4
6
  def build(obj = String.new)
5
7
  return obj.to_s unless block_given?
6
8
  yield builder = self.new
@@ -8,7 +10,7 @@ module String::Builder
8
10
  end
9
11
 
10
12
  def respond_to?(id, private = false)
11
- id.to_sym == :build ? true : super
13
+ %i[[] build].include?(id.to_sym) ? true : super
12
14
  end
13
15
 
14
16
  end
@@ -3,11 +3,11 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = "string-builder"
6
- spec.version = "2.1.1"
6
+ spec.version = "2.2.0"
7
7
  spec.authors = ["Edwin Onuonga"]
8
8
  spec.email = ["edwinonuonga@gmail.com"]
9
9
 
10
- spec.summary = %q{Modified and extended port of the String::Builder IO-style initializer for the String class of the Crystal programming language.}
10
+ spec.summary = %q{Modified and extended port of the String::Builder IO-style initializer for the String class of the Crystal programming language in the form of a Ruby gem refinement module.}
11
11
  spec.homepage = "https://www.github.com/eonu/string-builder"
12
12
  spec.license = "MIT"
13
13
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: string-builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Edwin Onuonga
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-06-07 00:00:00.000000000 Z
11
+ date: 2018-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -94,5 +94,6 @@ rubygems_version: 2.7.3
94
94
  signing_key:
95
95
  specification_version: 4
96
96
  summary: Modified and extended port of the String::Builder IO-style initializer for
97
- the String class of the Crystal programming language.
97
+ the String class of the Crystal programming language in the form of a Ruby gem refinement
98
+ module.
98
99
  test_files: []