Skip to content

Commit a54f6a3

Browse files
committed
String docs added
1 parent 4977ec3 commit a54f6a3

File tree

2 files changed

+157
-22
lines changed

2 files changed

+157
-22
lines changed

README.md

+143-15
Original file line numberDiff line numberDiff line change
@@ -18,128 +18,256 @@ methods like `match?` or `include?`, `empty?` use `_` instead of `?`.
1818
```
1919

2020

21-
### Methods supported
22-
#### in String Object
21+
## Methods supported
22+
### in String Object
23+
24+
#### *(n)
2325
```js
2426
"Ho! ".x(3) //=> "Ho! Ho! Ho! "
27+
```
2528

29+
#### capitalize
30+
```js
2631
"hello".capitalize() //=> "Hello"
32+
```
2733

28-
"abcdef".casecmp("abcde") //=> 1
29-
"aBcDeF".casecmp("abcdef") //=> 0
30-
"abcdef".casecmp("abcdefg") //=> -1
31-
"abcdef".casecmp("ABCDEF") //=> 0
32-
33-
34+
#### casecmp?(str)
3435

36+
```js
3537
"abcdef".casecmp_("abcde") //=> false
3638
"aBcDeF".casecmp_("abcdef") //=> true
3739
"abcdef".casecmp_("abcdefg") //=> false
3840
"abcdef".casecmp_("ABCDEF") //=> true
3941

4042

43+
```
44+
45+
#### center
46+
47+
```js
4148

4249
"hello".center(4) //=> "hello"
4350
"hello".center(20) //=> " hello "
4451
"hello".center(20, '123') //=> "1231231hello12312312"
52+
```
4553

54+
#### chr
4655

56+
```js
4757
"abcde".chr() //=> "a"
58+
```
59+
60+
#### downcase
61+
62+
```js
4863

4964
"hEllO".downcase() //=> "hello"
65+
```
66+
67+
#### upcase
68+
69+
```js
5070
"hEllO".upcase() //=> "HELLO"
71+
```
72+
73+
#### each_char {}
74+
75+
```js
5176

5277

5378
"hello".each_char(function(c){ console.log(c, ' ') })
5479
produces:
5580

5681
h e l l o
82+
```
83+
84+
#### empty?
85+
86+
```js
5787

5888
"hello".empty_ //=> false
5989
" ".empty _ //=> false
6090
"".empty_ //=> true
91+
```
92+
93+
#### end_with?(str)
94+
95+
```js
6196

6297

6398
"hello".end_with_("ello") //=> true
6499

65100
# returns true if one of the +suffixes+ matches.
66101
"hello".end_with_("heaven", "ello") //=> true
67102
"hello".end_with_("heaven", "paradise") //=> false
103+
```
104+
105+
#### gsub(patern, replacement_str)
106+
107+
```js
68108

69109

70110
"hello".gsub(/[aeiou]/, '*') //=> "h*ll*"
71111
"hello".gsub(/([aeiou])/, '<\1>') //=> "h<e>ll<o>"
112+
```
113+
114+
#### include?(str)
115+
116+
```js
72117

73118
"hello".include_("lo") //=> true
74119
"hello".include_("ol") //=> false
75120
"hello".include_('h') //=> true
121+
```
122+
123+
#### index
124+
125+
```js
76126

77127
"hello".index('e') //=> 1
78128
"hello".index('lo') //=> 3
79129
"hello".index('a') //=> nil
80130
"hello".index('e') //=> 1
131+
```
132+
133+
#### lines
134+
135+
```js
81136

82137
"hello\nworld\n".lines() //=> ["hello\n", "world\n"]
83138
"hello world".lines(' ') //=> ["hello ", " ", "world"]
84139
"hello\nworld\n".lines() //=> ["hello", "world"]
140+
```
141+
142+
#### lstrip
143+
144+
```js
85145

86146
" hello ".lstrip() //=> "hello "
87147
"hello".lstrip() //=> "hello"
148+
```
149+
150+
#### rstrip
151+
152+
```js
88153

89154
" hello ".rstrip() //=> " hello"
90155
"hello".rstrip() //=> "hello"
156+
```
157+
158+
#### match?(regex)
159+
160+
```js
91161

92162

93163
"Ruby".match_(/R.../) //=> true
94164
"Ruby".match_(/R.../, 1) //=> false
95165
"Ruby".match_(/P.../) //=> false
166+
```
167+
168+
#### succ
169+
170+
```js
96171

97172
"abcd".succ() //=> "abce"
98173
"THX1138".succ() //=> "THX1139"
99174
"<<koala>>".succ()
175+
```
176+
177+
#### next
178+
179+
```js
100180

101181
"abcd".next() //=> "abce"
102182
"THX1138".next() //=> "THX1139"
103183
"<<koala>>".next() //=> "<<koalb>>"
184+
```
185+
186+
#### ord
187+
188+
```js
104189

105190
"a".ord() //=> 49
191+
```
192+
193+
#### prepend
194+
195+
```js
106196

107197
// Non mutative
108198
a = "!"
109199
a.prepend("hello ", "world") //=> "hello world!"
200+
a.prepend("hello ", "world", 'shiva') //=> "hello world shiva!"
201+
```
202+
203+
#### reverse
204+
205+
```js
110206

111207
"stressed".reverse() //=> "desserts"
208+
```
209+
210+
#### scan(pattern)
211+
212+
```js
112213

113214
a = "cruel world"
114215
a.scan(/\w+/) //=> ["cruel", "world"]
115216
a.scan(/.../) //=> ["cru", "el ", "wor"]
116217
a.scan(/(...)/) //=> [["cru"], ["el "], ["wor"]]
117218
a.scan(/(..)(..)/) //=> [["cr", "ue"], ["l ", "wo"]]
219+
```
220+
221+
#### size
222+
223+
```js
118224

119225
"cruel world".size() //=> 11
226+
```
120227

228+
#### start_with?(str)
121229

230+
```js
122231
"hello".start_with_("hell") //=> true
123232
"hello".start_with_(/H/i) //=> true
124-
125-
# returns true if one of the prefixes matches.
126-
"hello".start_with_"heaven", "hell") //=> true
233+
// returns true if one of the prefixes matches.
234+
"hello".start_with_("heaven", "hell") //=> true
127235
"hello".start_with_("heaven", "paradise") //=> false
236+
```
237+
238+
#### sub(pattern)
239+
240+
```js
128241

129242
"hello".sub(/[aeiou]/, '*') //=> "h*llo"
243+
```
244+
245+
#### swapcase
246+
247+
```js
130248

131249
"Hello".swapcase() //=> "hELLO"
132250
"cYbEr_PuNk11".swapcase() //=> "CyBeR_pUnK11"
251+
```
252+
253+
#### to_f
254+
255+
```js
133256

134257
"123.45e1".to_f() //=> 1234.5
135258
"45.67 degrees".to_f() //=> 45.67
136259
"thx1138".to_f() //=> 0.0
260+
```
261+
262+
#### to_i
263+
264+
```js
137265

138-
"12345".to_i //=> 12345
139-
"99 red balloons".to_i //=> 99
140-
"0a".to_i //=> 0
266+
"12345".to_i() //=> 12345
267+
"99 red balloons".to_i() //=> 99
268+
"0a".to_i() //=> 0
141269
"0a".to_i(16) //=> 10
142-
"hello".to_i //=> 0
270+
"hello".to_i() //=> 0
143271
"1100101".to_i(2) //=> 101
144272
"1100101".to_i(8) //=> 294977
145273
"1100101".to_i(10) //=> 1100101

lib/ruby.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,14 @@ String.prototype.ord = function () {
205205
a = "!"
206206
a.prepend("hello ", "world") #=> "hello world!"
207207
*/
208-
String.prototype.prepend = function (collection_string) {
209-
if (Array.isArray(collection_string)) {
210-
return (collection_string.join(' ') + this);
211-
} else {
212-
return (collection_string.toString() + this)
208+
String.prototype.prepend = function () {
209+
var collection = [];
210+
211+
for (var i = 0; i < arguments.length; i++) {
212+
collection.push(arguments[i]);
213213
}
214+
215+
return (collection.join(' ') + this);
214216
};
215217

216218

@@ -255,8 +257,13 @@ String.prototype.size = function () {
255257
"hello".start_with?("heaven", "hell") #=> true
256258
"hello".start_with?("heaven", "paradise") #=> false
257259
*/
258-
String.prototype.start_with_ = function (str) {
259-
return (this.startsWith(str));
260+
String.prototype.start_with_ = function () {
261+
for (var i = 0; i < arguments.length; i++) {
262+
str = arguments[i];
263+
if (this.startsWith(str))
264+
return (true)
265+
}
266+
return (false);
260267
};
261268

262269

0 commit comments

Comments
 (0)