-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile.extension.html
1681 lines (1323 loc) · 82.7 KB
/
file.extension.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, user-scalable=no'>
<meta name='apple-touch-fullscreen' content='yes'>
<meta name='apple-mobile-web-app-capable' content='yes'>
<meta name='apple-mobile-web-app-status-bar-style' content='rgba(228,228,228,1.0)'>
<title>File: Extension — Ruby-2.6.10</title>
<link rel='stylesheet' type='text/css' href='../css/y_fonts.css' />
<link rel='stylesheet' type='text/css' href='../css/highlight.github.css' />
<link rel='stylesheet' type='text/css' href='../css/y_style.css' />
<link rel='stylesheet' type='text/css' href='../css/y_list.css' />
<link rel='stylesheet' type='text/css' href='../css/y_color.css' />
<script type='text/javascript'>
var pathId = "extension",
relpath = '';
var t2Info = {
CSEP: '.',
ISEP: '#',
NSEP: '::'
};
</script>
<script type='text/javascript' charset='utf-8' src='../js/highlight.pack.js'></script>
<script type='text/javascript' charset='utf-8' src='../js/y_app.js'></script>
</head>
<body>
<svg id='y_wait' class viewBox='0 0 90 90'></svg>
<div id='settings' class='hidden'></div>
<div id='y_list' class='d h'>
<header id='list_header'></header>
<nav id= 'list_nav' class='y_nav l_nav'>
<ul id='list_items'></ul>
</nav>
</div>
<div id='y_toc' class='f h'>
<header id='toc_header'></header>
<nav id= 'toc_nav' class='y_nav t_nav'>
<ol id='toc_items'></ol>
</nav>
</div>
<div id='y_main' tabindex='-1'>
<header id='y_header'>
<div id='y_menu'>
<a id='home_no_xhr' href='/'>Home</a> »
<a href='.'>Ruby-2.6.10</a> »
<a href='_index.html'>Index</a> »
<span class='title'><a id='t2_doc_top' href='#'>File: Extension ▲</a></span>
</div>
<a id='list_href' href="file_list.html"></a>
<div id='y_measure_em' class='y_measure'></div>
<div id='y_measure_vh' class='y_measure'></div>
<span id='y_measure_50pre' class='y_measure'><code>123456789_123456789_123456789_123456789_123456789_</code></span>
</header>
<div id='content' class='file'>
<p># extension.rdoc - -*- RDoc -*- created at: Mon Aug 7 16:45:54 JST 1995</p>
<h1 id="label-Creating+Extension+Libraries+for+Ruby">Creating Extension Libraries for Ruby</h1>
<p>This document explains how to make extension libraries for Ruby.</p>
<h2 id="label-Basic+Knowledge">Basic Knowledge</h2>
<p>In C, variables have types and data do not have types. In contrast, Ruby variables do not have a static type, and data themselves have types, so data will need to be converted between the languages.</p>
<p>Data in Ruby are represented by the C type ‘VALUE’. Each VALUE data has its data type.</p>
<p>To retrieve C data from a VALUE, you need to:</p>
<ol><li>
<p>Identify the VALUE’s data type</p>
</li><li>
<p>Convert the VALUE into C data</p>
</li></ol>
<p>Converting to the wrong data type may cause serious problems.</p>
<h3 id="label-Data+Types">Data Types</h3>
<p>The Ruby interpreter has the following data types:</p>
<dl class="rdoc-list note-list"><dt>T_NIL
<dd>
<p>nil</p>
</dd><dt>T_OBJECT
<dd>
<p>ordinary object</p>
</dd><dt>T_CLASS
<dd>
<p>class</p>
</dd><dt>T_MODULE
<dd>
<p>module</p>
</dd><dt>T_FLOAT
<dd>
<p>floating point number</p>
</dd><dt>T_STRING
<dd>
<p>string</p>
</dd><dt>T_REGEXP
<dd>
<p>regular expression</p>
</dd><dt>T_ARRAY
<dd>
<p>array</p>
</dd><dt>T_HASH
<dd>
<p>associative array</p>
</dd><dt>T_STRUCT
<dd>
<p>(Ruby) structure</p>
</dd><dt>T_BIGNUM
<dd>
<p>multi precision integer</p>
</dd><dt>T_FIXNUM
<dd>
<p>Fixnum(31bit or 63bit integer)</p>
</dd><dt>T_COMPLEX
<dd>
<p>complex number</p>
</dd><dt>T_RATIONAL
<dd>
<p>rational number</p>
</dd><dt>T_FILE
<dd>
<p>IO</p>
</dd><dt>T_TRUE
<dd>
<p>true</p>
</dd><dt>T_FALSE
<dd>
<p>false</p>
</dd><dt>T_DATA
<dd>
<p>data</p>
</dd><dt>T_SYMBOL
<dd>
<p>symbol</p>
</dd></dl>
<p>In addition, there are several other types used internally:</p>
<dl class="rdoc-list note-list"><dt>T_ICLASS
<dd>
<p>included module</p>
</dd><dt>T_MATCH
<dd>
<p>MatchData object</p>
</dd><dt>T_UNDEF
<dd>
<p>undefined</p>
</dd><dt>T_NODE
<dd>
<p>syntax tree node</p>
</dd><dt>T_ZOMBIE
<dd>
<p>object awaiting finalization</p>
</dd></dl>
<p>Most of the types are represented by C structures.</p>
<h3 id="label-Check+Data+Type+of+the+VALUE">Check Data Type of the VALUE</h3>
<p>The macro TYPE() defined in ruby.h shows the data type of the VALUE. TYPE() returns the constant number T_XXXX described above. To handle data types, your code will look something like this:</p>
<pre class="code ruby"><code class="ruby">switch (TYPE(obj)) {
case T_FIXNUM:
/* process Fixnum */
break;
case T_STRING:
/* process String */
break;
case T_ARRAY:
/* process Array */
break;
default:
/* raise exception */
rb_raise(rb_eTypeError, "not valid value");
break;
}</code></pre>
<p>There is the data type check function</p>
<pre class="code ruby"><code class="ruby">void Check_Type(VALUE value, int type)</code></pre>
<p>which raises an exception if the VALUE does not have the type specified.</p>
<p>There are also faster check macros for fixnums and nil.</p>
<pre class="code ruby"><code class="ruby"><span class='const'>FIXNUM_P</span>(<span class='id identifier rubyid_obj'>obj</span>)
<span class='const'>NIL_P</span>(<span class='id identifier rubyid_obj'>obj</span>)</code></pre>
<h3 id="label-Convert+VALUE+into+C+Data">Convert VALUE into C Data</h3>
<p>The data for type T_NIL, T_FALSE, T_TRUE are nil, false, true respectively. They are singletons for the data type. The equivalent C constants are: Qnil, Qfalse, Qtrue. Note that Qfalse is false in C also (i.e. 0), but not Qnil.</p>
<p>The T_FIXNUM data is a 31bit or 63bit length fixed integer. This size depends on the size of long: if long is 32bit then T_FIXNUM is 31bit, if long is 64bit then T_FIXNUM is 63bit. T_FIXNUM can be converted to a C integer by using the FIX2INT() macro or FIX2LONG(). Though you have to check that the data is really FIXNUM before using them, they are faster. FIX2LONG() never raises exceptions, but FIX2INT() raises RangeError if the result is bigger or smaller than the size of int. There are also NUM2INT() and NUM2LONG() which converts any Ruby numbers into C integers. These macros include a type check, so an exception will be raised if the conversion failed. NUM2DBL() can be used to retrieve the double float value in the same way.</p>
<p>You can use the macros StringValue() and StringValuePtr() to get a char* from a VALUE. StringValue(var) replaces var’s value with the result of “var.to_str()”. StringValuePtr(var) does the same replacement and returns the char* representation of var. These macros will skip the replacement if var is a String. Notice that the macros take only the lvalue as their argument, to change the value of var in place.</p>
<p>You can also use the macro named StringValueCStr(). This is just like StringValuePtr(), but always adds a NUL character at the end of the result. If the result contains a NUL character, this macro causes the ArgumentError exception. StringValuePtr() doesn’t guarantee the existence of a NUL at the end of the result, and the result may contain NUL.</p>
<p>Other data types have corresponding C structures, e.g. struct RArray for T_ARRAY etc. The VALUE of the type which has the corresponding structure can be cast to retrieve the pointer to the struct. The casting macro will be of the form RXXXX for each data type; for instance, RARRAY(obj). See “ruby.h”. However, we do not recommend to access RXXXX data directly because these data structures are complex. Use corresponding rb_xxx() functions to access the internal struct. For example, to access an entry of array, use rb_ary_entry(ary, offset) and rb_ary_store(ary, offset, obj).</p>
<p>There are some accessing macros for structure members, for example ‘RSTRING_LEN(str)’ to get the size of the Ruby String object. The allocated region can be accessed by ‘RSTRING_PTR(str)’.</p>
<p>Notice: Do not change the value of the structure directly, unless you are responsible for the result. This ends up being the cause of interesting bugs.</p>
<h3 id="label-Convert+C+Data+into+VALUE">Convert C Data into VALUE</h3>
<p>To convert C data to Ruby values:</p>
<dl class="rdoc-list note-list"><dt>FIXNUM
<dd>
<p>left shift 1 bit, and turn on its least significant bit (LSB).</p>
</dd><dt>Other pointer values
<dd>
<p>cast to VALUE.</p>
</dd></dl>
<p>You can determine whether a VALUE is a pointer or not by checking its LSB.</p>
<p>Notice: Ruby does not allow arbitrary pointer values to be a VALUE. They should be pointers to the structures which Ruby knows about. The known structures are defined in <ruby.h>.</p>
<p>To convert C numbers to Ruby values, use these macros:</p>
<dl class="rdoc-list note-list"><dt>INT2FIX()
<dd>
<p>for integers within 31bits.</p>
</dd><dt>INT2NUM()
<dd>
<p>for arbitrary sized integers.</p>
</dd></dl>
<p>INT2NUM() converts an integer into a Bignum if it is out of the FIXNUM range, but is a bit slower.</p>
<h3 id="label-Manipulating+Ruby+Data">Manipulating Ruby Data</h3>
<p>As I already mentioned, it is not recommended to modify an object’s internal structure. To manipulate objects, use the functions supplied by the Ruby interpreter. Some (not all) of the useful functions are listed below:</p>
<h4 id="label-String+Functions">String Functions</h4>
<dl class="rdoc-list note-list"><dt>rb_str_new(const char *ptr, long len)
<dd>
<p>Creates a new Ruby string.</p>
</dd><dt>rb_str_new2(const char *ptr)
<dt>rb_str_new_cstr(const char *ptr)
<dd>
<p>Creates a new Ruby string from a C string. This is equivalent to rb_str_new(ptr, strlen(ptr)).</p>
</dd><dt>rb_str_new_literal(const char *ptr)
<dd>
<p>Creates a new Ruby string from a C string literal.</p>
</dd><dt>rb_tainted_str_new(const char *ptr, long len)
<dd>
<p>Creates a new tainted Ruby string. Strings from external data sources should be tainted.</p>
</dd><dt>rb_tainted_str_new2(const char *ptr)
<dt>rb_tainted_str_new_cstr(const char *ptr)
<dd>
<p>Creates a new tainted Ruby string from a C string.</p>
</dd><dt>rb_sprintf(const char *format, …)
<dt>rb_vsprintf(const char *format, va_list ap)
<dd>
<p>Creates a new Ruby string with printf(3) format.</p>
<p>Note: In the format string, “%”PRIsVALUE can be used for Object#to_s (or Object#inspect if ‘+’ flag is set) output (and related argument must be a VALUE). Since it conflicts with “%i”, for integers in format strings, use “%d”.</p>
</dd><dt>rb_str_append(VALUE str1, VALUE str2)
<dd>
<p>Appends Ruby string str2 to Ruby string str1.</p>
</dd><dt>rb_str_cat(VALUE str, const char *ptr, long len)
<dd>
<p>Appends len bytes of data from ptr to the Ruby string.</p>
</dd><dt>rb_str_cat2(VALUE str, const char* ptr)
<dt>rb_str_cat_cstr(VALUE str, const char* ptr)
<dd>
<p>Appends C string ptr to Ruby string str. This function is equivalent to rb_str_cat(str, ptr, strlen(ptr)).</p>
</dd><dt>rb_str_catf(VALUE str, const char* format, …)
<dt>rb_str_vcatf(VALUE str, const char* format, va_list ap)
<dd>
<p>Appends C string format and successive arguments to Ruby string str according to a printf-like format. These functions are equivalent to rb_str_append(str, rb_sprintf(format, …)) and rb_str_append(str, rb_vsprintf(format, ap)), respectively.</p>
</dd><dt>rb_enc_str_new(const char *ptr, long len, rb_encoding *enc)
<dt>rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
<dd>
<p>Creates a new Ruby string with the specified encoding.</p>
</dd><dt>rb_enc_str_new_literal(const char *ptr, rb_encoding *enc)
<dd>
<p>Creates a new Ruby string from a C string literal with the specified encoding.</p>
</dd><dt>rb_usascii_str_new(const char *ptr, long len)
<dt>rb_usascii_str_new_cstr(const char *ptr)
<dd>
<p>Creates a new Ruby string with encoding US-ASCII.</p>
</dd><dt>rb_usascii_str_new_literal(const char *ptr)
<dd>
<p>Creates a new Ruby string from a C string literal with encoding US-ASCII.</p>
</dd><dt>rb_utf8_str_new(const char *ptr, long len)
<dt>rb_utf8_str_new_cstr(const char *ptr)
<dd>
<p>Creates a new Ruby string with encoding UTF-8.</p>
</dd><dt>rb_utf8_str_new_literal(const char *ptr)
<dd>
<p>Creates a new Ruby string from a C string literal with encoding UTF-8.</p>
</dd><dt>rb_str_resize(VALUE str, long len)
<dd>
<p>Resizes a Ruby string to len bytes. If str is not modifiable, this function raises an exception. The length of str must be set in advance. If len is less than the old length the content beyond len bytes is discarded, else if len is greater than the old length the content beyond the old length bytes will not be preserved but will be garbage. Note that RSTRING_PTR(str) may change by calling this function.</p>
</dd><dt>rb_str_set_len(VALUE str, long len)
<dd>
<p>Sets the length of a Ruby string. If str is not modifiable, this function raises an exception. This function preserves the content up to len bytes, regardless RSTRING_LEN(str). len must not exceed the capacity of str.</p>
</dd></dl>
<h4 id="label-Array+Functions">Array Functions</h4>
<dl class="rdoc-list note-list"><dt>rb_ary_new()
<dd>
<p>Creates an array with no elements.</p>
</dd><dt>rb_ary_new2(long len)
<dt>rb_ary_new_capa(long len)
<dd>
<p>Creates an array with no elements, allocating internal buffer for len elements.</p>
</dd><dt>rb_ary_new3(long n, …)
<dt>rb_ary_new_from_args(long n, …)
<dd>
<p>Creates an n-element array from the arguments.</p>
</dd><dt>rb_ary_new4(long n, VALUE *elts)
<dt>rb_ary_new_from_values(long n, VALUE *elts)
<dd>
<p>Creates an n-element array from a C array.</p>
</dd><dt>rb_ary_to_ary(VALUE obj)
<dd>
<p>Converts the object into an array. Equivalent to Object#to_ary.</p>
</dd></dl>
<p>There are many functions to operate an array. They may dump core if other types are given.</p>
<dl class="rdoc-list note-list"><dt>rb_ary_aref(int argc, const VALUE *argv, VALUE ary)
<dd>
<p>Equivalent to Array#[].</p>
</dd><dt>rb_ary_entry(VALUE ary, long offset)
<dd>
<p><a href=“offset”>ary</a></p>
</dd><dt>rb_ary_store(VALUE ary, long offset, VALUE obj)
<dd>
<p><a href=“offset”>ary</a> = obj</p>
</dd><dt>rb_ary_subseq(VALUE ary, long beg, long len)
<dd>
<p>ary[beg, len]</p>
</dd><dt>rb_ary_push(VALUE ary, VALUE val)
<dt>rb_ary_pop(VALUE ary)
<dt>rb_ary_shift(VALUE ary)
<dt>rb_ary_unshift(VALUE ary, VALUE val)
<dd>
<p>ary.push, ary.pop, ary.shift, ary.unshift</p>
</dd><dt>rb_ary_cat(VALUE ary, const VALUE *ptr, long len)
<dd>
<p>Appends len elements of objects from ptr to the array.</p>
</dd></dl>
<h2 id="label-Extending+Ruby+with+C">Extending Ruby with C</h2>
<h3 id="label-Adding+New+Features+to+Ruby">Adding New Features to Ruby</h3>
<p>You can add new features (classes, methods, etc.) to the Ruby interpreter. Ruby provides APIs for defining the following things:</p>
<ul><li>
<p>Classes, Modules</p>
</li><li>
<p>Methods, Singleton Methods</p>
</li><li>
<p>Constants</p>
</li></ul>
<h4 id="label-Class+and+Module+Definition">Class and Module Definition</h4>
<p>To define a class or module, use the functions below:</p>
<pre class="code ruby"><code class="ruby">VALUE rb_define_class(const char *name, VALUE super)
VALUE rb_define_module(const char *name)</code></pre>
<p>These functions return the newly created class or module. You may want to save this reference into a variable to use later.</p>
<p>To define nested classes or modules, use the functions below:</p>
<pre class="code ruby"><code class="ruby">VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
VALUE rb_define_module_under(VALUE outer, const char *name)</code></pre>
<h4 id="label-Method+and+Singleton+Method+Definition">Method and Singleton Method Definition</h4>
<p>To define methods or singleton methods, use these functions:</p>
<pre class="code ruby"><code class="ruby">void rb_define_method(VALUE klass, const char *name,
VALUE (*func)(ANYARGS), int argc)
void rb_define_singleton_method(VALUE object, const char *name,
VALUE (*func)(ANYARGS), int argc)</code></pre>
<p>The ‘argc’ represents the number of the arguments to the C function, which must be less than 17. But I doubt you’ll need that many.</p>
<p>If ‘argc’ is negative, it specifies the calling sequence, not number of the arguments.</p>
<p>If argc is -1, the function will be called as:</p>
<pre class="code ruby"><code class="ruby">VALUE func(int argc, VALUE *argv, VALUE obj)</code></pre>
<p>where argc is the actual number of arguments, argv is the C array of the arguments, and obj is the receiver.</p>
<p>If argc is -2, the arguments are passed in a Ruby array. The function will be called like:</p>
<pre class="code ruby"><code class="ruby">VALUE func(VALUE obj, VALUE args)</code></pre>
<p>where obj is the receiver, and args is the Ruby array containing actual arguments.</p>
<p>There are some more functions to define methods. One takes an ID as the name of method to be defined. See also ID or Symbol below.</p>
<pre class="code ruby"><code class="ruby">void rb_define_method_id(VALUE klass, ID name,
VALUE (*func)(ANYARGS), int argc)</code></pre>
<p>There are two functions to define private/protected methods:</p>
<pre class="code ruby"><code class="ruby">void rb_define_private_method(VALUE klass, const char *name,
VALUE (*func)(ANYARGS), int argc)
void rb_define_protected_method(VALUE klass, const char *name,
VALUE (*func)(ANYARGS), int argc)</code></pre>
<p>At last, rb_define_module_function defines a module function, which are private AND singleton methods of the module. For example, sqrt is a module function defined in the Math module. It can be called in the following way:</p>
<pre class="code ruby"><code class="ruby"><span class='const'>Math</span>.<span class='id identifier rubyid_sqrt'>sqrt</span>(<span class='int'>4</span>)</code></pre>
<p>or</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_include'>include</span> <span class='const'>Math</span>
<span class='id identifier rubyid_sqrt'>sqrt</span>(<span class='int'>4</span>)</code></pre>
<p>To define module functions, use:</p>
<pre class="code ruby"><code class="ruby">void rb_define_module_function(VALUE module, const char *name,
VALUE (*func)(ANYARGS), int argc)</code></pre>
<p>In addition, function-like methods, which are private methods defined in the Kernel module, can be defined using:</p>
<pre class="code ruby"><code class="ruby">void rb_define_global_function(const char *name, VALUE (*func)(ANYARGS), int argc)</code></pre>
<p>To define an alias for the method,</p>
<pre class="code ruby"><code class="ruby">void rb_define_alias(VALUE module, const char* new, const char* old);</code></pre>
<p>To define a reader/writer for an attribute,</p>
<pre class="code ruby"><code class="ruby">void rb_define_attr(VALUE klass, const char *name, int read, int write)</code></pre>
<p>To define and undefine the ‘allocate’ class method,</p>
<pre class="code ruby"><code class="ruby">void rb_define_alloc_func(VALUE klass, VALUE (*func)(VALUE klass));
void rb_undef_alloc_func(VALUE klass);</code></pre>
<p>func has to take the klass as the argument and return a newly allocated instance. This instance should be as empty as possible, without any expensive (including external) resources.</p>
<p>If you are overriding an existing method of any ancestor of your class, you may rely on:</p>
<pre class="code ruby"><code class="ruby">VALUE rb_call_super(int argc, const VALUE *argv)</code></pre>
<p>To achieve the receiver of the current scope (if no other way is available), you can use:</p>
<pre class="code ruby"><code class="ruby"><span class='const'>VALUE</span> <span class='id identifier rubyid_rb_current_receiver'>rb_current_receiver</span>(<span class='id identifier rubyid_void'>void</span>)</code></pre>
<h4 id="label-Constant+Definition">Constant Definition</h4>
<p>We have 2 functions to define constants:</p>
<pre class="code ruby"><code class="ruby">void rb_define_const(VALUE klass, const char *name, VALUE val)
void rb_define_global_const(const char *name, VALUE val)</code></pre>
<p>The former is to define a constant under specified class/module. The latter is to define a global constant.</p>
<h3 id="label-Use+Ruby+Features+from+C">Use Ruby Features from C</h3>
<p>There are several ways to invoke Ruby’s features from C code.</p>
<h4 id="label-Evaluate+Ruby+Programs+in+a+String">Evaluate Ruby Programs in a String</h4>
<p>The easiest way to use Ruby’s functionality from a C program is to evaluate the string as Ruby program. This function will do the job:</p>
<pre class="code ruby"><code class="ruby"><span class='const'>VALUE</span> <span class='id identifier rubyid_rb_eval_string'>rb_eval_string</span>(<span class='id identifier rubyid_const'>const</span> <span class='id identifier rubyid_char'>char</span> <span class='op'>*</span><span class='id identifier rubyid_str'>str</span>)</code></pre>
<p>Evaluation is done under the current context, thus current local variables of the innermost method (which is defined by Ruby) can be accessed.</p>
<p>Note that the evaluation can raise an exception. There is a safer function:</p>
<pre class="code ruby"><code class="ruby">VALUE rb_eval_string_protect(const char *str, int *state)</code></pre>
<p>It returns nil when an error occurred. Moreover, *state is zero if str was successfully evaluated, or nonzero otherwise.</p>
<h4 id="label-ID+or+Symbol">ID or Symbol</h4>
<p>You can invoke methods directly, without parsing the string. First I need to explain about ID. ID is the integer number to represent Ruby’s identifiers such as variable names. The Ruby data type corresponding to ID is Symbol. It can be accessed from Ruby in the form:</p>
<pre class="code ruby"><code class="ruby"><span class='symbeg'>:</span><span class='const'>Identifier</span></code></pre>
<p>or</p>
<pre class="code ruby"><code class="ruby"><span class='symbeg'>:"</span><span class='tstring_content'>any kind of string</span><span class='tstring_end'>"</span></span></code></pre>
<p>You can get the ID value from a string within C code by using</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_rb_intern'>rb_intern</span>(<span class='id identifier rubyid_const'>const</span> <span class='id identifier rubyid_char'>char</span> <span class='op'>*</span><span class='id identifier rubyid_name'>name</span>)
<span class='id identifier rubyid_rb_intern_str'>rb_intern_str</span>(<span class='const'>VALUE</span> <span class='id identifier rubyid_name'>name</span>)</code></pre>
<p>You can retrieve ID from Ruby object (Symbol or String) given as an argument by using</p>
<pre class="code ruby"><code class="ruby">rb_to_id(VALUE symbol)
rb_check_id(volatile VALUE *name)
rb_check_id_cstr(const char *name, long len, rb_encoding *enc)</code></pre>
<p>These functions try to convert the argument to a String if it was not a Symbol nor a String. The second function stores the converted result into *name, and returns 0 if the string is not a known symbol. After this function returned a non-zero value, *name is always a Symbol or a String, otherwise it is a String if the result is 0. The third function takes NUL-terminated C string, not Ruby VALUE.</p>
<p>You can retrieve Symbol from Ruby object (Symbol or String) given as an argument by using</p>
<pre class="code ruby"><code class="ruby">rb_to_symbol(VALUE name)
rb_check_symbol(volatile VALUE *namep)
rb_check_symbol_cstr(const char *ptr, long len, rb_encoding *enc)</code></pre>
<p>These functions are similar to above functions except that these return a Symbol instead of an ID.</p>
<p>You can convert C ID to Ruby Symbol by using</p>
<pre class="code ruby"><code class="ruby"><span class='const'>VALUE</span> <span class='const'>ID2SYM</span>(<span class='const'>ID</span> <span class='id identifier rubyid_id'>id</span>)</code></pre>
<p>and to convert Ruby Symbol object to ID, use</p>
<pre class="code ruby"><code class="ruby"><span class='const'>ID</span> <span class='const'>SYM2ID</span>(<span class='const'>VALUE</span> <span class='id identifier rubyid_symbol'>symbol</span>)</code></pre>
<h4 id="label-Invoke+Ruby+Method+from+C">Invoke Ruby Method from C</h4>
<p>To invoke methods directly, you can use the function below</p>
<pre class="code ruby"><code class="ruby">VALUE rb_funcall(VALUE recv, ID mid, int argc, ...)</code></pre>
<p>This function invokes a method on the recv, with the method name specified by the symbol mid.</p>
<h4 id="label-Accessing+the+Variables+and+Constants">Accessing the Variables and Constants</h4>
<p>You can access class variables and instance variables using access functions. Also, global variables can be shared between both environments. There’s no way to access Ruby’s local variables.</p>
<p>The functions to access/modify instance variables are below:</p>
<pre class="code ruby"><code class="ruby">VALUE rb_ivar_get(VALUE obj, ID id)
VALUE rb_ivar_set(VALUE obj, ID id, VALUE val)</code></pre>
<p>id must be the symbol, which can be retrieved by rb_intern().</p>
<p>To access the constants of the class/module:</p>
<pre class="code ruby"><code class="ruby">VALUE rb_const_get(VALUE obj, ID id)</code></pre>
<p>See also Constant Definition above.</p>
<h2 id="label-Information+Sharing+Between+Ruby+and+C">Information Sharing Between Ruby and C</h2>
<h3 id="label-Ruby+Constants+That+Can+Be+Accessed+From+C">Ruby Constants That Can Be Accessed From C</h3>
<p>As stated in section 1.3, the following Ruby constants can be referred from C.</p>
<dl class="rdoc-list note-list"><dt>Qtrue
<dt>Qfalse
<dd>
<p>Boolean values. Qfalse is false in C also (i.e. 0).</p>
</dd><dt>Qnil
<dd>
<p>Ruby nil in C scope.</p>
</dd></dl>
<h3 id="label-Global+Variables+Shared+Between+C+and+Ruby">Global Variables Shared Between C and Ruby</h3>
<p>Information can be shared between the two environments using shared global variables. To define them, you can use functions listed below:</p>
<pre class="code ruby"><code class="ruby">void rb_define_variable(const char *name, VALUE *var)</code></pre>
<p>This function defines the variable which is shared by both environments. The value of the global variable pointed to by ‘var’ can be accessed through Ruby’s global variable named ‘name’.</p>
<p>You can define read-only (from Ruby, of course) variables using the function below.</p>
<pre class="code ruby"><code class="ruby">void rb_define_readonly_variable(const char *name, VALUE *var)</code></pre>
<p>You can define hooked variables. The accessor functions (getter and setter) are called on access to the hooked variables.</p>
<pre class="code ruby"><code class="ruby">void rb_define_hooked_variable(const char *name, VALUE *var,
VALUE (*getter)(), void (*setter)())</code></pre>
<p>If you need to supply either setter or getter, just supply 0 for the hook you don’t need. If both hooks are 0, rb_define_hooked_variable() works just like rb_define_variable().</p>
<p>The prototypes of the getter and setter functions are as follows:</p>
<pre class="code ruby"><code class="ruby">VALUE (*getter)(ID id, VALUE *var);
void (*setter)(VALUE val, ID id, VALUE *var);</code></pre>
<p>Also you can define a Ruby global variable without a corresponding C variable. The value of the variable will be set/get only by hooks.</p>
<pre class="code ruby"><code class="ruby">void rb_define_virtual_variable(const char *name,
VALUE (*getter)(), void (*setter)())</code></pre>
<p>The prototypes of the getter and setter functions are as follows:</p>
<pre class="code ruby"><code class="ruby">VALUE (*getter)(ID id);
void (*setter)(VALUE val, ID id);</code></pre>
<h3 id="label-Encapsulate+C+Data+into+a+Ruby+Object">Encapsulate C Data into a Ruby Object</h3>
<p>Sometimes you need to expose your struct in the C world as a Ruby object. In a situation like this, making use of the TypedData_XXX macro family, the pointer to the struct and the Ruby object can be mutually converted.</p>
<p>– The old (non-Typed) Data_XXX macro family has been deprecated. In the future version of Ruby, it is possible old macros will not work. ++</p>
<h4 id="label-C+struct+to+Ruby+object">C struct to Ruby object</h4>
<p>You can convert sval, a pointer to your struct, into a Ruby object with the next macro.</p>
<pre class="code ruby"><code class="ruby"><span class='const'>TypedData_Wrap_Struct</span>(<span class='id identifier rubyid_klass'>klass</span><span class='comma'>,</span> <span class='id identifier rubyid_data_type'>data_type</span><span class='comma'>,</span> <span class='id identifier rubyid_sval'>sval</span>)</code></pre>
<p>TypedData_Wrap_Struct() returns a created Ruby object as a VALUE.</p>
<p>The klass argument is the class for the object. data_type is a pointer to a const rb_data_type_t which describes how Ruby should manage the struct.</p>
<p>It is recommended that klass derives from a special class called Data (rb_cData) but not from Object or other ordinal classes. If it doesn’t, you have to call rb_undef_alloc_func(klass).</p>
<p>rb_data_type_t is defined like this. Let’s take a look at each member of the struct.</p>
<pre class="code ruby"><code class="ruby">typedef struct rb_data_type_struct rb_data_type_t;
struct rb_data_type_struct {
const char *wrap_struct_name;
struct {
void (*dmark)(void*);
void (*dfree)(void*);
size_t (*dsize)(const void *);
void *reserved[2];
} function;
const rb_data_type_t *parent;
void *data;
VALUE flags;
};</code></pre>
<p>wrap_struct_name is an identifier of this instance of the struct. It is basically used for collecting and emitting statistics. So the identifier must be unique in the process, but doesn’t need to be valid as a C or Ruby identifier.</p>
<p>These dmark / dfree functions are invoked during GC execution. No object allocations are allowed during it, so do not allocate ruby objects inside them.</p>
<p>dmark is a function to mark Ruby objects referred from your struct. It must mark all references from your struct with rb_gc_mark or its family if your struct keeps such references.</p>
<p>– Note that it is recommended to avoid such a reference. ++</p>
<p>dfree is a function to free the pointer allocation. If this is -1, the pointer will be just freed.</p>
<p>dsize calculates memory consumption in bytes by the struct. Its parameter is a pointer to your struct. You can pass 0 as dsize if it is hard to implement such a function. But it is still recommended to avoid 0.</p>
<p>You have to fill reserved and parent with 0.</p>
<p>You can fill “data” with an arbitrary value for your use. Ruby does nothing with the member.</p>
<p>flags is a bitwise-OR of the following flag values. Since they require deep understanding of garbage collector in Ruby, you can just set 0 to flags if you are not sure.</p>
<dl class="rdoc-list note-list"><dt>RUBY_TYPED_FREE_IMMEDIATELY
<dd>
<p>This flag makes the garbage collector immediately invoke dfree() during GC when it need to free your struct. You can specify this flag if the dfree never unlocks Ruby’s internal lock (GVL).</p>
<p>If this flag is not set, Ruby defers invokation of dfree() and invokes dfree() at the same time as finalizers.</p>
</dd><dt>RUBY_TYPED_WB_PROTECTED
<dd>
<p>It shows that implementation of the object supports write barriers. If this flag is set, Ruby is better able to do garbage collection of the object.</p>
<p>When it is set, however, you are responsible for putting write barriers in all implementations of methods of that object as appropriate. Otherwise Ruby might crash while running.</p>
<p>More about write barriers can be found in “Generational GC” in Appendix D.</p>
</dd></dl>
<p>You can allocate and wrap the structure in one step.</p>
<pre class="code ruby"><code class="ruby"><span class='const'>TypedData_Make_Struct</span>(<span class='id identifier rubyid_klass'>klass</span><span class='comma'>,</span> <span class='id identifier rubyid_type'>type</span><span class='comma'>,</span> <span class='id identifier rubyid_data_type'>data_type</span><span class='comma'>,</span> <span class='id identifier rubyid_sval'>sval</span>)</code></pre>
<p>This macro returns an allocated Data object, wrapping the pointer to the structure, which is also allocated. This macro works like:</p>
<pre class="code ruby"><code class="ruby">(<span class='id identifier rubyid_sval'>sval</span> <span class='op'>=</span> <span class='const'>ZALLOC</span>(<span class='id identifier rubyid_type'>type</span>)<span class='comma'>,</span> <span class='const'>TypedData_Wrap_Struct</span>(<span class='id identifier rubyid_klass'>klass</span><span class='comma'>,</span> <span class='id identifier rubyid_data_type'>data_type</span><span class='comma'>,</span> <span class='id identifier rubyid_sval'>sval</span>))</code></pre>
<p>Arguments klass and data_type work like their counterparts in TypedData_Wrap_Struct(). A pointer to the allocated structure will be assigned to sval, which should be a pointer of the type specified.</p>
<h4 id="label-Ruby+object+to+C+struct">Ruby object to C struct</h4>
<p>To retrieve the C pointer from the Data object, use the macro TypedData_Get_Struct().</p>
<pre class="code ruby"><code class="ruby">TypedData_Get_Struct(obj, type, &data_type, sval)</code></pre>
<p>A pointer to the structure will be assigned to the variable sval.</p>
<p>See the example below for details.</p>
<h2 id="label-Example+-+Creating+the+dbm+Extension">Example - Creating the dbm Extension</h2>
<p>OK, here’s the example of making an extension library. This is the extension to access DBMs. The full source is included in the ext/ directory in the Ruby’s source tree.</p>
<h3 id="label-Make+the+Directory">Make the Directory</h3>
<pre class="code ruby"><code class="ruby">% mkdir ext/dbm</code></pre>
<p>Make a directory for the extension library under ext directory.</p>
<h3 id="label-Design+the+Library">Design the Library</h3>
<p>You need to design the library features, before making it.</p>
<h3 id="label-Write+the+C+Code">Write the C Code</h3>
<p>You need to write C code for your extension library. If your library has only one source file, choosing “LIBRARY.c” as a file name is preferred. On the other hand, in case your library has multiple source files, avoid choosing “LIBRARY.c” for a file name. It may conflict with an intermediate file “LIBRARY.o” on some platforms. Note that some functions in mkmf library described below generate a file “conftest.c” for checking with compilation. You shouldn’t choose “conftest.c” as a name of a source file.</p>
<p>Ruby will execute the initializing function named “Init_LIBRARY” in the library. For example, “Init_dbm()” will be executed when loading the library.</p>
<p>Here’s the example of an initializing function.</p>
<pre class="code ruby"><code class="ruby">void
Init_dbm(void)
{
/* define DBM class */
VALUE cDBM = rb_define_class("DBM", rb_cObject);
/* DBM includes Enumerable module */
rb_include_module(cDBM, rb_mEnumerable);
/* DBM has class method open(): arguments are received as C array */
rb_define_singleton_method(cDBM, "open", fdbm_s_open, -1);
/* DBM instance method close(): no args */
rb_define_method(cDBM, "close", fdbm_close, 0);
/* DBM instance method []: 1 argument */
rb_define_method(cDBM, "[]", fdbm_fetch, 1);
/* ... */
/* ID for a instance variable to store DBM data */
id_dbm = rb_intern("dbm");
}</code></pre>
<p>The dbm extension wraps the dbm struct in the C environment using TypedData_Make_Struct.</p>
<pre class="code ruby"><code class="ruby">struct dbmdata {
int di_size;
DBM *di_dbm;
};
static const rb_data_type_t dbm_type = {
"dbm",
{0, free_dbm, memsize_dbm,},
0, 0,
RUBY_TYPED_FREE_IMMEDIATELY,
};
obj = TypedData_Make_Struct(klass, struct dbmdata, &dbm_type, dbmp);</code></pre>
<p>This code wraps the dbmdata structure into a Ruby object. We avoid wrapping DBM* directly, because we want to cache size information.</p>
<p>To retrieve the dbmdata structure from a Ruby object, we define the following macro:</p>
<pre class="code ruby"><code class="ruby">#define GetDBM(obj, dbmp) do {\
TypedData_Get_Struct((obj), struct dbmdata, &dbm_type, (dbmp));\
if ((dbmp) == 0) closed_dbm();\
if ((dbmp)->di_dbm == 0) closed_dbm();\
} while (0)</code></pre>
<p>This sort of complicated macro does the retrieving and close checking for the DBM.</p>
<p>There are three kinds of way to receive method arguments. First, methods with a fixed number of arguments receive arguments like this:</p>
<pre class="code ruby"><code class="ruby">static VALUE
fdbm_delete(VALUE obj, VALUE keystr)
{
/* ... */
}</code></pre>
<p>The first argument of the C function is the self, the rest are the arguments to the method.</p>
<p>Second, methods with an arbitrary number of arguments receive arguments like this:</p>
<pre class="code ruby"><code class="ruby">static VALUE
fdbm_s_open(int argc, VALUE *argv, VALUE klass)
{
/* ... */
if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
mode = 0666; /* default value */
}
/* ... */
}</code></pre>
<p>The first argument is the number of method arguments, the second argument is the C array of the method arguments, and the third argument is the receiver of the method.</p>
<p>You can use the function rb_scan_args() to check and retrieve the arguments. The third argument is a string that specifies how to capture method arguments and assign them to the following VALUE references.</p>
<p>You can just check the argument number with rb_check_arity(), this is handy in the case you want to treat the arguments as a list.</p>
<p>The following is an example of a method that takes arguments by Ruby’s array:</p>
<pre class="code ruby"><code class="ruby">static VALUE
thread_initialize(VALUE thread, VALUE args)
{
/* ... */
}</code></pre>
<p>The first argument is the receiver, the second one is the Ruby array which contains the arguments to the method.</p>
<p><strong>Notice</strong>: GC should know about global variables which refer to Ruby’s objects, but are not exported to the Ruby world. You need to protect them by</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_void'>void</span> <span class='id identifier rubyid_rb_global_variable'>rb_global_variable</span>(<span class='const'>VALUE</span> <span class='op'>*</span><span class='id identifier rubyid_var'>var</span>)</code></pre>
<p>or the objects themselves by</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_void'>void</span> <span class='id identifier rubyid_rb_gc_register_mark_object'>rb_gc_register_mark_object</span>(<span class='const'>VALUE</span> <span class='id identifier rubyid_object'>object</span>)</code></pre>
<h3 id="label-Prepare+extconf.rb">Prepare extconf.rb</h3>
<p>If the file named extconf.rb exists, it will be executed to generate Makefile.</p>
<p>extconf.rb is the file for checking compilation conditions etc. You need to put</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>mkmf</span><span class='tstring_end'>'</span></span></code></pre>
<p>at the top of the file. You can use the functions below to check various conditions.</p>
<pre class="code ruby"><code class="ruby">have_macro(macro[, headers[, opt]]): check whether macro is defined
have_library(lib[, func[, headers[, opt]]]): check whether library containing function exists
find_library(lib[, func, *paths]): find library from paths
have_func(func[, headers[, opt]): check whether function exists
have_var(var[, headers[, opt]]): check whether variable exists
have_header(header[, preheaders[, opt]]): check whether header file exists
find_header(header, *paths): find header from paths
have_framework(fw): check whether framework exists (for MacOS X)
have_struct_member(type, member[, headers[, opt]]): check whether struct has member
have_type(type[, headers[, opt]]): check whether type exists
find_type(type, opt, *headers): check whether type exists in headers
have_const(const[, headers[, opt]]): check whether constant is defined
check_sizeof(type[, headers[, opts]]): check size of type
check_signedness(type[, headers[, opts]]): check signedness of type
convertible_int(type[, headers[, opts]]): find convertible integer type
find_executable(bin[, path]): find executable file path
create_header(header): generate configured header
create_makefile(target[, target_prefix]): generate Makefile</code></pre>
<p>See MakeMakefile for full documentation of these functions.</p>
<p>The value of the variables below will affect the Makefile.</p>
<pre class="code ruby"><code class="ruby">$CFLAGS: included in CFLAGS make variable (such as -O)
$CPPFLAGS: included in CPPFLAGS make variable (such as -I, -D)
$LDFLAGS: included in LDFLAGS make variable (such as -L)
$objs: list of object file names</code></pre>
<p>Normally, the object files list is automatically generated by searching source files, but you must define them explicitly if any sources will be generated while building.</p>
<p>If a compilation condition is not fulfilled, you should not call “create_makefile”. The Makefile will not be generated, compilation will not be done.</p>
<h3 id="label-Prepare+Depend+-28Optional-29">Prepare Depend (Optional)</h3>
<p>If the file named depend exists, Makefile will include that file to check dependencies. You can make this file by invoking</p>
<pre class="code ruby"><code class="ruby">% gcc -MM *.c > depend</code></pre>
<p>It’s harmless. Prepare it.</p>
<h3 id="label-Generate+Makefile">Generate Makefile</h3>
<p>Try generating the Makefile by:</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_ruby'>ruby</span> <span class='id identifier rubyid_extconf'>extconf</span>.<span class='id identifier rubyid_rb'>rb</span></code></pre>
<p>If the library should be installed under vendor_ruby directory instead of site_ruby directory, use –vendor option as follows.</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_ruby'>ruby</span> <span class='id identifier rubyid_extconf'>extconf</span>.<span class='id identifier rubyid_rb'>rb</span> <span class='op'>-</span><span class='op'>-</span><span class='id identifier rubyid_vendor'>vendor</span></code></pre>
<p>You don’t need this step if you put the extension library under the ext directory of the ruby source tree. In that case, compilation of the interpreter will do this step for you.</p>
<h3 id="label-Run+make">Run make</h3>
<p>Type</p>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_make'>make</span></code></pre>
<p>to compile your extension. You don’t need this step either if you have put the extension library under the ext directory of the ruby source tree.</p>
<h3 id="label-Debug">Debug</h3>
<p>You may need to rb_debug the extension. Extensions can be linked statically by adding the directory name in the ext/Setup file so that you can inspect the extension with the debugger.</p>
<h3 id="label-Done-21+Now+You+Have+the+Extension+Library">Done! Now You Have the Extension Library</h3>
<p>You can do anything you want with your library. The author of Ruby will not claim any restrictions on your code depending on the Ruby API. Feel free to use, modify, distribute or sell your program.</p>
<h2 id="label-Appendix+A.+Ruby+Source+Files+Overview">Appendix A. Ruby Source Files Overview</h2>
<h3 id="label-Ruby+Language+Core">Ruby Language Core</h3>
<dl class="rdoc-list note-list"><dt>class.c
<dd>
<p>classes and modules</p>
</dd><dt>error.c
<dd>
<p>exception classes and exception mechanism</p>
</dd><dt>gc.c
<dd>
<p>memory management</p>
</dd><dt>load.c
<dd>
<p>library loading</p>
</dd><dt>object.c
<dd>
<p>objects</p>
</dd><dt>variable.c
<dd>
<p>variables and constants</p>
</dd></dl>
<h3 id="label-Ruby+Syntax+Parser">Ruby Syntax Parser</h3>
<dl class="rdoc-list note-list"><dt>parse.y
<dd>
<p>grammar definition</p>
</dd><dt>parse.c
<dd>
<p>automatically generated from parse.y</p>
</dd><dt>defs/keywords
<dd>
<p>reserved keywords</p>
</dd><dt>lex.c
<dd>
<p>automatically generated from keywords</p>
</dd></dl>
<h3 id="label-Ruby+Evaluator+-28a.k.a.+YARV-29">Ruby Evaluator (a.k.a. YARV)</h3>
<pre class="code ruby"><code class="ruby">compile.c
eval.c
eval_error.c
eval_jump.c
eval_safe.c
insns.def : definition of VM instructions
iseq.c : implementation of VM::ISeq
thread.c : thread management and context switching
thread_win32.c : thread implementation
thread_pthread.c : ditto
vm.c
vm_dump.c
vm_eval.c
vm_exec.c
vm_insnhelper.c
vm_method.c
defs/opt_insns_unif.def : instruction unification
defs/opt_operand.def : definitions for optimization
#=> insn*.inc : automatically generated
#=> opt*.inc : automatically generated
#=> vm.inc : automatically generated</code></pre>
<h3 id="label-Regular+Expression+Engine+-28Oniguruma-29">Regular Expression Engine (Oniguruma)</h3>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_regex'>regex</span>.<span class='id identifier rubyid_c'>c</span>
<span class='id identifier rubyid_regcomp'>regcomp</span>.<span class='id identifier rubyid_c'>c</span>
<span class='id identifier rubyid_regenc'>regenc</span>.<span class='id identifier rubyid_c'>c</span>
<span class='id identifier rubyid_regerror'>regerror</span>.<span class='id identifier rubyid_c'>c</span>
<span class='id identifier rubyid_regexec'>regexec</span>.<span class='id identifier rubyid_c'>c</span>
<span class='id identifier rubyid_regparse'>regparse</span>.<span class='id identifier rubyid_c'>c</span>
<span class='id identifier rubyid_regsyntax'>regsyntax</span>.<span class='id identifier rubyid_c'>c</span></code></pre>
<h3 id="label-Utility+Functions">Utility Functions</h3>
<dl class="rdoc-list note-list"><dt>debug.c
<dd>
<p>debug symbols for C debugger</p>
</dd><dt>dln.c
<dd>
<p>dynamic loading</p>
</dd><dt>st.c
<dd>
<p>general purpose hash table</p>
</dd><dt>strftime.c
<dd>
<p>formatting times</p>
</dd><dt>util.c
<dd>
<p>misc utilities</p>
</dd></dl>
<h3 id="label-Ruby+Interpreter+Implementation">Ruby Interpreter Implementation</h3>
<pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_dmyext'>dmyext</span>.<span class='id identifier rubyid_c'>c</span>
<span class='id identifier rubyid_dmydln'>dmydln</span>.<span class='id identifier rubyid_c'>c</span>
<span class='id identifier rubyid_dmyencoding'>dmyencoding</span>.<span class='id identifier rubyid_c'>c</span>
<span class='id identifier rubyid_id'>id</span>.<span class='id identifier rubyid_c'>c</span>
<span class='id identifier rubyid_inits'>inits</span>.<span class='id identifier rubyid_c'>c</span>
<span class='id identifier rubyid_main'>main</span>.<span class='id identifier rubyid_c'>c</span>
<span class='id identifier rubyid_ruby'>ruby</span>.<span class='id identifier rubyid_c'>c</span>
<span class='id identifier rubyid_version'>version</span>.<span class='id identifier rubyid_c'>c</span>
<span class='id identifier rubyid_gem_prelude'>gem_prelude</span>.<span class='id identifier rubyid_rb'>rb</span>
<span class='id identifier rubyid_prelude'>prelude</span>.<span class='id identifier rubyid_rb'>rb</span></code></pre>
<h3 id="label-Class+Library">Class Library</h3>
<dl class="rdoc-list note-list"><dt>array.c
<dd>
<p>Array</p>
</dd><dt>bignum.c
<dd>
<p>Bignum</p>
</dd><dt>compar.c
<dd>
<p>Comparable</p>
</dd><dt>complex.c