lazy_string 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d7ef828d0d6e270b3c763849c5503bef8434ac156e487d517b528ec64d5c4ab4
4
+ data.tar.gz: 4cbba8428b89845019b048a1bbb700f2dcaeda71c853f849ef9462914e364293
5
+ SHA512:
6
+ metadata.gz: ee16dc18e4c5035ee131cfa0c92e3bc1c5f6fcd1f7cacc5c9c3a8632c20fecc1683781c2498340e2a322c8c077eea2beea90238ac0bb3839a025c96fb136730c
7
+ data.tar.gz: '048e4f0fea648cb3655db2042b174ddc13e81d3f9933d166542e3bf61c15e1e005cf47a1b1594fb2833c88f2c1adad4c0b445a6e077a1d8ac62d95cdb75fc1f5'
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2018 Aaron Beckerman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be included
12
+ in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ LazyString
2
+
3
+ LazyString is a Ruby class for computing strings only when necessary -- that
4
+ is, when #to_str or #to_s is called. This is useful when computing the string
5
+ is expensive, but the string is unlikely to be used: an error message passed as
6
+ a method argument, for example.
7
+
8
+ Thanks to duck typing and Ruby's standard conversion protocols, you can pass
9
+ an instance of LazyString to many methods that ask for a string.
10
+
11
+
12
+ Usage
13
+
14
+ LazyString is packaged as a RubyGem. Once you've installed it and required the
15
+ file, you can create a lazy string by passing a block to LazyString.new:
16
+
17
+ ls = LazyString.new { "answer = #{expensive_computation}" }
18
+
19
+ The block (and therefore expensive_computation) will not be called immediately.
20
+ But later, if #to_str or #to_s is called on the lazy string, the block will be
21
+ called with no arguments in order to create the real string.
22
+
23
+ # String interpolation calls #to_s automatically.
24
+ s = "Lazy string computes this: #{ls}"
25
+
26
+ Once the string is computed, it will be saved, so any future invocations of
27
+ #to_str or #to_s will not perform the computation again.
28
+
29
+
30
+ Compatibility
31
+
32
+ This works on version 1.8.7 and later of Matz's Ruby Interpreter. (At the time
33
+ of this writing, 2.2.3 is the current version.)
34
+
35
+
36
+ Author
37
+
38
+ This software was written by Aaron Beckerman.
39
+
40
+
41
+ License
42
+
43
+ This software is distributed under the MIT License (also known as the Expat
44
+ License). See the LICENSE.txt file for details.
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "lazy_string"
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'lazy_string'
8
+ gem.version = LazyString::VERSION
9
+ gem.authors = ['Aaron Beckerman']
10
+ gem.description = %q{This library provides LazyString, a class for computing strings only when necessary -- that is, when #to_str or #to_s is called. This is useful when computing the string is expensive but the string is unlikely to be used: an error message passed as a method argument, for example.}
11
+ gem.summary = %q{A Ruby library for lazily computing strings.}
12
+ gem.license = 'MIT'
13
+ gem.required_ruby_version = '>= 1.8.7'
14
+
15
+ gem.files = %w{LICENSE.txt README.txt lazy_string.gemspec lib/lazy_string.rb test/lazy_string_test.rb}
16
+ gem.executables = %w{}
17
+ gem.test_files = %w{test/lazy_string_test.rb}
18
+ gem.require_paths = %w{lib}
19
+ end
@@ -0,0 +1,47 @@
1
+ ##
2
+ # LazyString is a class for computing strings only when necessary -- that is,
3
+ # when #to_str or #to_s is called. This is useful when computing the string is
4
+ # expensive, but the string is unlikely to be used: an error message passed as
5
+ # a method argument, for example.
6
+ #
7
+ class LazyString
8
+ ##
9
+ # The version string.
10
+ #
11
+ VERSION = '0.3.0'
12
+
13
+ ##
14
+ # Creates a new lazy string. The block argument will be saved for later use
15
+ # by #to_str. If no block argument is given, it will default to a block that
16
+ # returns an empty string.
17
+ #
18
+ def initialize(&block)
19
+ @block = block || lambda { '' }
20
+ end
21
+
22
+ ##
23
+ # A synonym for #to_str.
24
+ #
25
+ def to_s
26
+ to_str
27
+ end
28
+
29
+ ##
30
+ # Returns the computed string. It will compute the string by calling (with no
31
+ # arguments) the block passed to ::new. If the block's return value responds
32
+ # to the \to_str message, \to_str will be called on it to create the string.
33
+ # Otherwise, \to_s will be called on it. The computed string will be saved,
34
+ # so subsequent calls of this method will not cause the block to be called
35
+ # again.
36
+ #
37
+ def to_str
38
+ @str ||= begin
39
+ result = @block.call
40
+ if result.respond_to?(:to_str)
41
+ result.to_str
42
+ else
43
+ result.to_s
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,55 @@
1
+ require 'lazy_string'
2
+
3
+ Class == LazyString.class or fail
4
+
5
+ String == LazyString::VERSION.class or fail
6
+
7
+ lambda do
8
+ obj = Object.new
9
+ LazyString.new { obj.freeze }
10
+ !obj.frozen? or fail
11
+ end.call
12
+
13
+ LazyString.new.respond_to?(:to_str) or fail
14
+
15
+ LazyString.new.respond_to?(:to_s) or fail
16
+
17
+ '' == LazyString.new.to_str or fail
18
+
19
+ lambda do
20
+ obj = Object.new
21
+ def obj.to_str; 'to_str' end
22
+ def obj.to_s; 'to_s' end
23
+ 'to_str' == LazyString.new { obj }.to_str or fail
24
+ end.call
25
+
26
+ lambda do
27
+ obj = Object.new
28
+ def obj.to_s; 'to_s' end
29
+ !obj.respond_to?(:to_str) or fail
30
+ 'to_s' == LazyString.new { obj }.to_str or fail
31
+ end.call
32
+
33
+ '42' == LazyString.new { 2 * 21 }.to_s or fail
34
+
35
+ lambda do
36
+ arr = []
37
+ ls = LazyString.new { arr << nil; 'foo' }
38
+ ls.to_str
39
+ 'foo' == ls.to_str or fail
40
+ 1 == arr.length or fail
41
+ end.call
42
+
43
+ lambda do
44
+ ex = StandardError.new
45
+ ls = LazyString.new { raise(ex) }
46
+ begin
47
+ ls.to_str
48
+ rescue
49
+ ex.equal?($!) or fail
50
+ else
51
+ fail
52
+ end
53
+ end.call
54
+
55
+ puts 'Test finished.'
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lazy_string
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Beckerman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'This library provides LazyString, a class for computing strings only
14
+ when necessary -- that is, when #to_str or #to_s is called. This is useful when
15
+ computing the string is expensive but the string is unlikely to be used: an error
16
+ message passed as a method argument, for example.'
17
+ email:
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - LICENSE.txt
23
+ - README.txt
24
+ - lazy_string.gemspec
25
+ - lib/lazy_string.rb
26
+ - test/lazy_string_test.rb
27
+ homepage:
28
+ licenses:
29
+ - MIT
30
+ metadata: {}
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: 1.8.7
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project:
47
+ rubygems_version: 2.7.6
48
+ signing_key:
49
+ specification_version: 4
50
+ summary: A Ruby library for lazily computing strings.
51
+ test_files:
52
+ - test/lazy_string_test.rb