Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

first commit

  • Loading branch information...
commit 6d20ee6b0a86b9dd1d1ad94808ec25538d0488d3 0 parents
Sean Johnson authored
2  factorial/extconf.rb
@@ -0,0 +1,2 @@
+require 'mkmf'
+create_makefile("factorial_in_c")
32 factorial/factorial.c
@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <ruby.h>
+
+static VALUE factorial(VALUE class, VALUE an_int_V) {
+
+ int n = FIX2INT(an_int_V);
+
+ // An int is too small, not safe, of course!
+ int fact = 1;
+
+ while (n > 1) {
+ fact = fact * n;
+ n = n - 1;
+ }
+
+ return INT2FIX(fact);
+
+}
+
+void Init_factorial_in_c() {
+
+ // Create a Ruby module
+ VALUE factorialModule = rb_define_module("FactorialModule");
+
+ // Create a Ruby class in the module
+ VALUE factorialClass = rb_define_class_under(factorialModule, "FactorialInC", rb_cObject);
+
+ // Add a class method to the Ruby class
+ int arg_count = 1;
+ rb_define_module_function(factorialClass, "factorial", factorial, arg_count);
+
+}
21 factorial/factorial.rb
@@ -0,0 +1,21 @@
+require 'factorial_in_c'
+require 'benchmark'
+
+include FactorialModule
+
+puts "A number to get the factorial?"
+number = gets.to_i
+
+factorial = 0
+puts "Ruby factorial computation time:\n" + Benchmark.measure {
+ factorial = 1;
+ for i in 1..number
+ factorial = factorial * i;
+ end
+}.to_s
+puts "Ruby says: #{factorial}"
+
+puts "C factorial computation time:\n" + Benchmark.measure {
+ factorial = FactorialInC.factorial(number)
+}.to_s
+puts "C says: #{factorial}"
2  hello/extconf.rb
@@ -0,0 +1,2 @@
+require 'mkmf'
+create_makefile("hello_in_c")
21 hello/hello.c
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#include <ruby.h>
+
+static VALUE hello() {
+ printf("raleigh.rb\n");
+ return Qnil;
+}
+
+void Init_hello_in_c() {
+
+ // Create a Ruby module
+ VALUE helloModule = rb_define_module("HelloModule");
+
+ // Create a Ruby class in the module
+ VALUE helloClass = rb_define_class_under(helloModule, "HelloInC", rb_cObject);
+
+ // Add a class method to the Ruby class
+ int arg_count = 0;
+ rb_define_module_function(helloClass, "hello", hello, arg_count);
+
+}
6 hello/hello.rb
@@ -0,0 +1,6 @@
+require 'hello_in_c'
+
+include HelloModule
+
+puts "Hello"
+HelloInC.hello
2  passing/extconf.rb
@@ -0,0 +1,2 @@
+require 'mkmf'
+create_makefile("passing_in_c")
70 passing/passing.c
@@ -0,0 +1,70 @@
+#include <stdio.h>
+#include <ruby.h>
+
+static VALUE passString(VALUE class, VALUE a_string_V) {
+
+ // Get the Ruby String into a C char array
+ // 1.8
+ //char* input = RSTRING(a_string_V)->ptr;
+
+ //1.9
+ char* input = RSTRING_PTR(a_string_V);
+ printf("C sees: %s", input);
+
+ // Reverse the string
+ char temp;
+ int i = 0;
+ int j = strlen(input) - 1;
+ for(; i<j; i++, j--) {
+ temp = input[i];
+ input[i] = input[j];
+ input[j] = temp;
+ }
+
+ // Return a Ruby string from the C char array
+ VALUE reverse = rb_str_new2(input);
+ return reverse;
+}
+
+static VALUE passArray(VALUE class, VALUE an_array_V) {
+
+ // Unpack the passed in array
+ int i = 0;
+ printf("C sees: ");
+ for(; i < RARRAY_LEN(an_array_V); i++) {
+ VALUE an_int_V = RARRAY_PTR(an_array_V)[i];
+ int an_int = FIX2INT(an_int_V);
+ printf("%i ", an_int);
+ }
+ printf("\n");
+
+ // Build up an array of arrays to return
+ VALUE return_array = rb_ary_new2(5);
+ for (i=0;i<5;i++) {
+ VALUE inner_array = rb_ary_new2(2);
+ rb_ary_store(inner_array, 0, INT2FIX(i));
+ rb_ary_store(inner_array, 1, INT2FIX(i+1));
+ rb_ary_store(return_array, i, inner_array);
+ }
+
+ return return_array;
+
+}
+
+void Init_passing_in_c() {
+
+ // Create a Ruby module
+ VALUE passingModule = rb_define_module("PassingModule");
+
+ // Create a Ruby class in the module
+ VALUE passingClass = rb_define_class_under(passingModule, "PassingInC", rb_cObject);
+
+ // Add a class method to the Ruby clas
+ int arg_count = 1;
+ rb_define_module_function(passingClass, "pass_string", passString, arg_count);
+
+ // Add a class method to the Ruby class
+ arg_count = 1;
+ rb_define_module_function(passingClass, "pass_array", passArray, arg_count);
+
+}
14 passing/passing.rb
@@ -0,0 +1,14 @@
+require 'passing_in_c'
+
+include PassingModule
+
+puts "Enter a string for C:"
+string = gets
+result = PassingInC.pass_string(string)
+puts "C says: #{result}"
+
+result = PassingInC.pass_array([1,2,3])
+puts "Returned array of arrays:"
+for array in result do
+ puts "#{array[0]}, #{array[1]}"
+end
2  random/extconf.rb
@@ -0,0 +1,2 @@
+require 'mkmf'
+create_makefile("random_in_c")
21 random/random.c
@@ -0,0 +1,21 @@
+#include <stdio.h>
+#include <ruby.h>
+
+static VALUE random_number(VALUE class, VALUE an_int_V) {
+ int max = FIX2INT(an_int_V);
+ return INT2FIX(rand() % max);
+}
+
+void Init_random_in_c() {
+
+ // Create a Ruby module
+ VALUE randomModule = rb_define_module("RandomModule");
+
+ // Create a Ruby class in the module
+ VALUE randomClass = rb_define_class_under(randomModule, "RandomInC", rb_cObject);
+
+ // Add a class method to the Ruby class
+ int arg_count = 1;
+ rb_define_module_function(randomClass, "random_number", random_number, arg_count);
+
+}
8 random/random.rb
@@ -0,0 +1,8 @@
+require 'random_in_c'
+
+include RandomModule
+
+puts "A random number less than?"
+max = gets
+rand = RandomInC.random_number(max.to_i)
+puts "C says: #{rand}"
Please sign in to comment.
Something went wrong with that request. Please try again.