forked from samuel-velazquez/gpm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdout.html
303 lines (226 loc) · 12.4 KB
/
stdout.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<!DOCTYPE html>
<html>
<head>
<title>$stdout</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<link rel="stylesheet" media="all" href="assets/css/style.css" />
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.1.4/semantic.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.8.0/styles/tomorrow-night.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.8.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.1.4/semantic.min.js"></script>
<script src="assets/js/javascript.js"></script>
</head>
<body>
<div class="ui left push vertical inverted sidebar menu">
<a class="item menu-link" href="index.html">
Index
</a>
<a class="item menu-link" href="integer.html">
Integer
</a>
<a class="item menu-link" href="percent_strings.html">
Percent Strings
</a>
<a class="item menu-link" href="stdout.html">
Stdout
</a>
<a class="item menu-link" href="string.html">
String
</a>
</div>
<div class='pusher'>
<div id='menu-buttons' class='ui fixed top sticky'>
<div class='ui basic button icon' id='menu-button'>
<i class='icon sidebar'></i>
</div>
<a href='https://github.com/crookedneighbor/ruby-node/blob/master/src/stdout.js' class='ui basic button icon' data-content="View Source Code">
<i class='icon code'></i>
</a>
</div>
<div id="background" class="hljs"></div>
<div id="container" class="ui container">
<div class="ui middle aligned two column grid">
<h2 class="ui icon header center aligned column">
<i class="comment outline icon"></i>
$stdout
<div class="sub header">Get the stdout!</div>
</h2>
<div class='column'></div>
</div>
<div class="sections">
<div id="Methods" class="section-1 ui middle aligned two column grid">
<div class="annotation column">
<a class='section-link' href="#Methods">
<i class="linkify icon"></i>
</a>
<h1 id="methods">Methods</h1>
<ul>
<li><a href="#putc">putc</a></li>
<li><a href="#puts">puts</a></li>
</ul>
</div>
<div class="content column hljs javascript"><div class='highlight'><pre><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-class"><span class="hljs-keyword">class</span> $<span class="hljs-title">Stdout</span> </span>{
constructor () {
<span class="hljs-keyword">this</span>.putc = putc;
<span class="hljs-keyword">this</span>.puts = puts;
}
}</pre></div></div>
</div>
<div id="putc" class="section-2 ui middle aligned two column grid">
<div class="annotation column">
<a class='section-link' href="#putc">
<i class="linkify icon"></i>
</a>
<h1 id="putc-">putc()</h1>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { putc } <span class="hljs-keyword">from</span> <span class="hljs-string">'ruby'</span>;
</code></pre>
<p>Prints a character to stdout. Only prints the first letter of the string and only takes in a single string or number.</p>
<pre><code class="lang-js">putc(<span class="hljs-string">'ABC'</span>);
<span class="hljs-comment">// A</span>
</code></pre>
<p>Prints the character equivalent of the number that is passed in.</p>
<pre><code class="lang-js">putc(<span class="hljs-number">65</span>);
<span class="hljs-comment">// A</span>
</code></pre>
<p>Does not print new new lines.</p>
<pre><code class="lang-js">putc(<span class="hljs-string">'A'</span>);
putc(<span class="hljs-string">'B'</span>);
putc(<span class="hljs-string">'C'</span>);
<span class="hljs-comment">// ABC</span>
</code></pre>
<p>Returns the original argument</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> word = putc(<span class="hljs-string">'ABC'</span>);
word; <span class="hljs-comment">// 'ABC'</span>
</code></pre>
</div>
<div class="content column hljs javascript"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">putc</span> (<span class="hljs-params">arg</span>) </span>{
<span class="hljs-keyword">if</span> (<span class="hljs-built_in">arguments</span>.length !== <span class="hljs-number">1</span>) {
<span class="hljs-keyword">throw</span> <span class="hljs-string">`ArgumentError: wrong number of arguments (<span class="hljs-subst">${arguments.length}</span> for 1)`</span>;
}
<span class="hljs-keyword">if</span> (!(isNumber(arg) || isString(arg))) {
<span class="hljs-keyword">throw</span> <span class="hljs-string">`no implicit conversion of <span class="hljs-subst">${typeof arg}</span> into Integer (TypeError)`</span>;
}
<span class="hljs-keyword">let</span> output;
<span class="hljs-keyword">if</span> (isString(arg)) {
output = arg.slice(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>);
} <span class="hljs-keyword">else</span> {
output = <span class="hljs-built_in">String</span>.fromCharCode(arg);
}
process.stdout.write(output);
<span class="hljs-keyword">return</span> arg;
}</pre></div></div>
</div>
<div id="puts" class="section-3 ui middle aligned two column grid">
<div class="annotation column">
<a class='section-link' href="#puts">
<i class="linkify icon"></i>
</a>
<h1 id="puts-">puts()</h1>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> {puts} <span class="hljs-keyword">from</span> <span class="hljs-string">'ruby'</span>;
</code></pre>
<h3 id="strings">Strings</h3>
<p>Prints the string to stdout</p>
<pre><code class="lang-js">puts(<span class="hljs-string">'this is a test'</span>);
<span class="hljs-comment">// this is a test</span>
</code></pre>
<h3 id="numbers">Numbers</h3>
<p>Prints the number to stdout</p>
<pre><code class="lang-js">puts(<span class="hljs-number">10</span>);
<span class="hljs-comment">// 10</span>
</code></pre>
<h3 id="boolean">Boolean</h3>
<p>Prints the boolean to stdout</p>
<pre><code class="lang-js">puts(<span class="hljs-literal">true</span>);
<span class="hljs-comment">// true</span>
</code></pre>
<h3 id="regex">Regex</h3>
<p>Prints the regex in the form (?-mix:the_regex)</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> regex = <span class="hljs-regexp">/^this is a test$/</span>;
puts(regex);
<span class="hljs-comment">// (?-mix:^this is a test$)</span>
</code></pre>
<h3 id="undefined-or-null">Undefined or Null</h3>
<p>Prints an empty string to stdout</p>
<pre><code class="lang-js">puts(<span class="hljs-literal">null</span>);
<span class="hljs-comment">// ''</span>
puts(<span class="hljs-literal">undefined</span>);
<span class="hljs-comment">// ''</span>
</code></pre>
<h3 id="nan">NaN</h3>
<p>Prints the string NaN</p>
<pre><code class="lang-js">puts(<span class="hljs-literal">NaN</span>);
<span class="hljs-comment">// NaN</span>
puts(<span class="hljs-number">0</span>/<span class="hljs-number">0</span>);
<span class="hljs-comment">// NaN</span>
</code></pre>
<h3 id="error">Error</h3>
<p>Prints the string NaN</p>
<pre><code class="lang-js">puts(<span class="hljs-literal">NaN</span>);
<span class="hljs-comment">// NaN</span>
puts(<span class="hljs-number">0</span>/<span class="hljs-number">0</span>);
<span class="hljs-comment">// NaN</span>
</code></pre>
<h3 id="arrays">Arrays</h3>
<p>Prints each element to stdout on a new line</p>
<pre><code class="lang-js">puts([<span class="hljs-string">'this'</span>, <span class="hljs-string">'is'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'test'</span>]);
<span class="hljs-comment">// this</span>
<span class="hljs-comment">// is</span>
<span class="hljs-comment">// a</span>
<span class="hljs-comment">// test</span>
</code></pre>
<h3 id="plain-objects">Plain Objects</h3>
<p>Prints the string version of objects that inherit directly from the Object prototype</p>
<pre><code class="lang-js">puts({this_is: <span class="hljs-string">'a test'</span>});
<span class="hljs-comment">// { this_is: 'a test' }</span>
</code></pre>
<h3 id="other-objects">Other Objects</h3>
<p>Prints the object’s construtor’s name in the form: <code>#<ClassName></code>. Unfortunately, javascript does not expose the memory location of the object, so that can not be added as a reference id to the ouput.</p>
<pre><code class="lang-js">puts(ruby.$stdout);
<span class="hljs-comment">// #<$Stdout></span>
</code></pre>
<h3 id="functions">Functions</h3>
<p>Prints the function’s construtor’s name in the form: <code>#<Name></code>.</p>
<pre><code class="lang-js">puts(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{ <span class="hljs-keyword">return</span> <span class="hljs-string">'this is a test'</span>});
<span class="hljs-comment">// #<Function></span>
</code></pre>
<h3 id="multiple-arguments">Multiple Arguments</h3>
<p>If multiple arguments are passed in, they’ll be printed to stdout separated by new lines</p>
<pre><code class="lang-js">puts(<span class="hljs-string">'this'</span>, <span class="hljs-string">'is'</span>, <span class="hljs-string">'a'</span>, <span class="hljs-string">'test'</span>, ruby.$stdout);
<span class="hljs-comment">// this</span>
<span class="hljs-comment">// is</span>
<span class="hljs-comment">// a</span>
<span class="hljs-comment">// test</span>
<span class="hljs-comment">// #<$Stdout></span>
</code></pre>
</div>
<div class="content column hljs javascript"><div class='highlight'><pre><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">puts</span> (<span class="hljs-params"></span>) </span>{
each(<span class="hljs-built_in">arguments</span>, (arg) => {
<span class="hljs-keyword">if</span> (isString(arg) || isNumber(arg) || isBoolean(arg)) {
_printWithNewLine(<span class="hljs-built_in">String</span>(arg));
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (isNull(arg) || isUndefined(arg)) {
_printWithNewLine(<span class="hljs-string">''</span>);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (isRegExp(arg)) {
<span class="hljs-keyword">let</span> stringifiedRegEx = _convertRegexToString(arg);
_printWithNewLine(stringifiedRegEx);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (arg <span class="hljs-keyword">instanceof</span> <span class="hljs-built_in">Error</span>) {
<span class="hljs-keyword">let</span> stringifiedError = _convertErrorToString(arg);
_printWithNewLine(stringifiedError);
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (isArray(arg)) {
each(arg, (element) => {
puts(element);
});
} <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!isPlainObject(arg) && isObject(arg)) {
_printWithNewLine(<span class="hljs-string">`#<<span class="hljs-subst">${arg.constructor.name}</span>>`</span>);
} <span class="hljs-keyword">else</span> {
<span class="hljs-built_in">console</span>.log(arg); <span class="hljs-comment">// hack to make plain objects print semi-correctly</span>
}
});
}</pre></div></div>
</div>
</div>
</div>
</div>
</body>
</html>