open_string 0.0.2
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 +15 -0
- data/lib/open_string.rb +56 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NWVkODk0MGIyY2IzNzhiZmZhMmEzYzJiYWIwMWJkYmU2YzFiZTUzYg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
OTg1NjBjODAxY2VjYWI3NzY5NDRmNjJmMDg1OGZkMGRiM2RmNDZkMw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZTI5ZDNjMzYxMDdkMjc3N2M1YmM0YTcxMDJhZTAxMGY5N2MzNmFkNzJlOTY3
|
10
|
+
ZWM5NGQzM2E1MzFhYzE5ZGU5ZmU3ZTBjMWI2ZTU4ZmFlYmQwY2I0OGI5YTdh
|
11
|
+
NmFlZGRhY2UwNmRhMTU4NjkzMGNlZjUwODZmMWU1YWY5NzcwODE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MjBmYWE2ZDI0Yzk4NmM4Yjg4NDZkNWZkZjY4ZTdlZTIxYzJmYTM5NmQ0ZjZl
|
14
|
+
MzRkYjI0YmU0ZTgxMjhkZmY2MTg3ZjkwZjM2Zjg0MWNmYjZmYjcyOWViMWQ5
|
15
|
+
NzgzNmM3YmUwYTlmODEwZTFiZGU1ODlkZDE1MzZhNzQwZjg5MGM=
|
data/lib/open_string.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Public: Provides extension methods for the built-in String class.
|
4
|
+
module OpenString
|
5
|
+
# Public: Determines if self has the same sequence of characters as evaluated
|
6
|
+
# from index 0 to self.length and from self.length to 0
|
7
|
+
#
|
8
|
+
# Returns true iff self contains the same sequence of characters forwards as
|
9
|
+
# it does backwards.
|
10
|
+
def palindrome?
|
11
|
+
self == reverse
|
12
|
+
end
|
13
|
+
|
14
|
+
# Methods meant to be members of the String class need to go in here. The
|
15
|
+
# machinery here is a little odd: since the +randomize+ declaration isn't
|
16
|
+
# prefaced with a +self+, it doesn't look static. Once +String+ is set to
|
17
|
+
# +extend OpenInteger::ClassMethods+, this is treated like a static String
|
18
|
+
# method.
|
19
|
+
#
|
20
|
+
# If this inner module wasn't created and, instead, String was set to
|
21
|
+
# +include OpenString+, consumers would need to call static methods via
|
22
|
+
# +OpenString+ like so: +OpenString.randomize(10, CharacterSets.all)+.
|
23
|
+
module ClassMethods
|
24
|
+
# Public: Generates a randomized string of a length specified by +length+.
|
25
|
+
#
|
26
|
+
# +length+:: The number of characters to include in the string
|
27
|
+
# +*charsets+:: Arrays of character sets to use in generating the random
|
28
|
+
# string. This can take array literals, variables, or constants. Some
|
29
|
+
# character sets are predefined in test/support/character_sets.rb
|
30
|
+
#
|
31
|
+
# ---
|
32
|
+
# :section: Examples
|
33
|
+
# # Generates a 10-character string using only lowercase letters
|
34
|
+
# str = String.randomize(10, CharacterSets.lowercase)
|
35
|
+
#
|
36
|
+
# # Generates a 5-character string using only ['a', 's', 'd', '1', '2', '3']
|
37
|
+
# str = String.randomize(5, %w(a s d 1 2 3))
|
38
|
+
# ---
|
39
|
+
def randomize(length, *charsets)
|
40
|
+
raise(ArgumentError,
|
41
|
+
'Do not pass nil for length.') if length.nil?
|
42
|
+
raise(ArgumentError,
|
43
|
+
'Length must be a non-zero positive integer.') if length < 1
|
44
|
+
raise(ArgumentError,
|
45
|
+
'Specify a character set.') if charsets.empty?
|
46
|
+
|
47
|
+
Array.new(length) { charsets.flatten.sample }.join
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Public: Mixing OpenString extensions into String.
|
53
|
+
class String
|
54
|
+
include OpenString
|
55
|
+
extend OpenString::ClassMethods
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: open_string
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason Conner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Various extensions for the String class
|
14
|
+
email: jason.r.conner@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/open_string.rb
|
20
|
+
homepage: http://rubygems.org/gems/open_string
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.4.5
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: open_string
|
44
|
+
test_files: []
|