as-extensions 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +5 -9
- data/ext/string.rb +47 -21
- metadata +3 -3
data/README.md
CHANGED
@@ -11,18 +11,14 @@ Using this gem will make some classes behave differently than what you
|
|
11
11
|
are used to. The Time and DateTime classes are especially concerned.
|
12
12
|
Read the RDoc if you use them.
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
1) Install Jeweler (http://github.com/technicalpickles/jeweler).
|
14
|
+
Also, tests are desperately needed...
|
17
15
|
|
18
|
-
|
19
|
-
|
20
|
-
3) Run:
|
16
|
+
## How To Install
|
21
17
|
|
22
|
-
|
23
|
-
gem install pkg/*.gem
|
18
|
+
ASE is now on RubyGems, so:
|
24
19
|
|
25
|
-
|
20
|
+
1. `gem install as-extensions`
|
21
|
+
2. Add `require 'as-extensions'` to your code.
|
26
22
|
|
27
23
|
## Copyright
|
28
24
|
|
data/ext/string.rb
CHANGED
@@ -18,65 +18,91 @@
|
|
18
18
|
#++
|
19
19
|
|
20
20
|
String.class_eval do
|
21
|
-
|
22
|
-
ASE_ARRAY_METHODS = %w{first first= head head= init last last= tail}.map_m(:to_sym)
|
23
|
-
|
21
|
+
|
24
22
|
class << self
|
25
|
-
|
26
|
-
attr_accessor :ase_array_methods
|
27
|
-
|
23
|
+
|
28
24
|
# Load a string from a file or a URL.
|
29
25
|
def from(file_or_url)
|
30
26
|
ASE::String::from(file_or_url)
|
31
27
|
end
|
32
|
-
|
28
|
+
|
33
29
|
# Return an alphanumeric string.
|
34
30
|
def rand_alphanum(n=1, secure=false)
|
35
31
|
@ase_alphanum ||= [('a'..'z'), ('A'..'Z'), ('0'..'9')].map{ |x| x.to_a }.flatten
|
36
32
|
Array.new(n){ @ase_alphanum.pick(secure) }.join
|
37
33
|
end
|
38
|
-
|
34
|
+
|
39
35
|
# Return an hexadecimal string.
|
40
36
|
def rand_hex(n=1, secure=false)
|
41
37
|
@ase_hex ||= "0123456789abcdef".chars
|
42
38
|
Array.new(n){ @ase_hex.pick(secure) }.join
|
43
39
|
end
|
44
|
-
|
40
|
+
|
45
41
|
end # class << self
|
46
|
-
|
42
|
+
|
47
43
|
alias :camelcase_noase :camelcase
|
48
44
|
# Same as camelcase from ActiveSupport
|
49
45
|
# but dashes are considered as underscores.
|
50
46
|
def camelcase
|
51
47
|
underscore.camelcase_noase
|
52
48
|
end
|
53
|
-
|
49
|
+
|
54
50
|
alias :dasherize_noase :dasherize
|
55
51
|
# Same as underscore from ActiveSupport
|
56
52
|
# with dashes instead of underscores.
|
57
53
|
def dasherize
|
58
54
|
underscore.dasherize_noase
|
59
55
|
end
|
60
|
-
|
56
|
+
|
61
57
|
# Consider the String as an Array of chars for a few methods
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
58
|
+
|
59
|
+
def first
|
60
|
+
self[0]
|
61
|
+
end
|
62
|
+
|
63
|
+
def first=(x)
|
64
|
+
raise IndexError, 'empty string' if empty?
|
65
|
+
self[0] = x
|
66
|
+
end
|
67
|
+
|
68
|
+
alias :head :first
|
69
|
+
alias :head= :first=
|
70
|
+
|
71
|
+
def init
|
72
|
+
self[0..-2]
|
73
|
+
end
|
74
|
+
|
75
|
+
def init=(x)
|
76
|
+
self[0..-2] = x
|
77
|
+
end
|
78
|
+
|
79
|
+
def last
|
80
|
+
self[-1]
|
81
|
+
end
|
82
|
+
|
83
|
+
def last=(x)
|
84
|
+
raise IndexError, 'empty string' if empty?
|
85
|
+
self[-1] = x
|
68
86
|
end
|
69
|
-
|
87
|
+
|
88
|
+
def tail
|
89
|
+
self[1..-1]
|
90
|
+
end
|
91
|
+
|
92
|
+
def tail=(x)
|
93
|
+
self[1..-1] = x
|
94
|
+
end
|
95
|
+
|
70
96
|
# Helper to convert a raw string to a sane identifier with dashes and ASCII letters.
|
71
97
|
# Interpolation is here to force String type, to_s won't always work.
|
72
98
|
def sanitize_dashes
|
73
99
|
"#{self.to_slug.approximate_ascii.to_ascii.normalize.to_s}"
|
74
100
|
end
|
75
|
-
|
101
|
+
|
76
102
|
# Sometimes calling puts as a method can be useful
|
77
103
|
def puts
|
78
104
|
Kernel::puts self
|
79
105
|
end
|
80
106
|
alias :p :puts
|
81
|
-
|
107
|
+
|
82
108
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Pierre Chapuis
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-
|
17
|
+
date: 2011-12-07 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|