Hello World with Ruby extension library for the time being


I will briefly explain how to call a library written in C from Ruby. There are some sites that explain it, including the official one, but I wanted you to tell me more simply ...


https://ruby-doc.org/core-2.5.3/doc/extension_ja_rdoc.html https://qiita.com/suketa/items/ab6b88093de4a54b3b06

Sample made


I will make it at once

Create a library for Ruby in C

As shown in the sample C code shown above. Define a class with an Init function (you can also define a module), create a method definition and the corresponding function. In the method, it is written at the beginning of the official document, but since the usage of types is different between Ruby and C, use a dedicated function when interacting with each other. In other words, it looks like the following.


    return rb_str_new("Hello World", 11);

Make a Makefile and make

I don't write the Makefile myself. You can make it by creating extconf.rb and then ruby extconf.rb. This is also included in the sample. (I think it can be executed if Ruby is installed, but maybe ruby-dev is necessary?) The module name (class name?) Specified by create_makefile () does not have to match the case of the file name. So I used hello.c in lowercase for the C file name and Hello for the class name like a type name. ruby extconf.rb and when the Makefile is created, make. By the way, even if you write extconf.rb before creating the C file, you may get a Makefile that cannot be made.

Try to run

Since it's a big deal, I'll write up to the point where I'll try it. I wrote it in the sample, but you can do it as follows.

require "./Hello"

hello = Hello::new

#=> [:say, :instance_variable_set, :instance_variable_defined?, ...
#    ^There is a method I made here.

#=> "Hello World"

#=> String

Background I made

At first, I thought that I should execute the program made in C as a command from Ruby, but I learned that the overhead of popen is quite large, so I decided to make a shared library. If you popopen in a single shot, you won't see much overhead. However, I think that there will be a big difference depending on whether you execute it with popen about 1000 times or load the library and execute it. (I think it depends on the execution environment) The following is the result of the experiment.

The result of executing the C program to Hello World 1000 times with popen:
                 user     system      total        real
Case01       0.125000   0.296875   2.625000 ( 11.617396)

The result of executing the library I made this time 1000 times:
                 user     system      total        real
Case01       0.000000   0.000000   0.000000 (  0.000436)

1000 libraries I made this time*Result of running 1000 times:
                 user     system      total        real
Case01       0.109375   0.015625   0.125000 (  0.130412)

in conclusion

As a person who uses Ruby, you may try to find the cause of the heavy pop of Ruby ....

Recommended Posts

Hello World with Ruby extension library for the time being
Use Java external library for the time being
"Hello world" for ImageJ with Eclipse
[First Java] Make something that works with Intellij for the time being
Programming for the first time in my life Java 1st Hello World
Install Amazon Corretto (preview) for the time being
Run Dataflow, Java, streaming for the time being
Hello World with Micronaut
Oreore certificate https (2020/12/19) for the first time with nginx
Try running Spring Cloud Config for the time being
Command to try using Docker for the time being
A memo to do for the time being when building CentOS 6 series with VirtualBox
Hello World with Spring Boot
Hello World with Spring Boot!
Hello World with VS Code!
Access Web API on Android with Get and process Json (Java for the time being)
Hello World with Spring Boot
Hello World with SpringBoot / Gradle
With the software I've been making for a long time ...
Hello, World! With Asakusa Framework!
Modeling a Digimon with DDD for the first time Part 1
Java14 came out, so I tried record for the time being
Try Hello World with the minimum configuration of Heroku Java spring-boot
A memorial service for the library used when competing in Ruby
Spring Boot for the first time
Hello World for ImageJ Java Plugin
Until "Hello World" with Spring Boot
Programming with ruby (on the way)
Hello world with Kotlin and JavaFX
Hello World with Docker and C
(Intellij) Hello World with Spring Boot
Hello World with GlassFish 5.1 + Servlet + JSP
Create PDF with itext7 ~ Hello World ~
Spring AOP for the first time
Hello World with GWT 2.8.2 and Maven
Let's experience the authorization code grant flow with Spring Security OAuth-Part 2: Creating an app for the time being
Building a DLNA server on Ubuntu (just move for the time being)
Java12 came out, so I tried the switch expression for the time being
Glassfish tuning list that I want to keep for the time being
Getting Started with Ruby for Java Engineers
Introduction to java for the first time # 2
Hello World with Eclipse + Spring Boot + Maven
Hello, World! In the bootstrap loader area
Hello world with Java template engine Thymeleaf
Java development with Codenvy: Hello World! Run
"Hello, World!" With Kotlin + CLI in 5 minutes
Hello world with Kotlin and Tornado FX
Learning for the first time java [Introduction]
How Spring Security works with Hello World
(IntelliJ + gradle) Hello World with Spring Boot
Time shift measures with Docker for Windows
Use the l method for time notation
The training for newcomers was "Make an app!", So I made an app for the time being.
I want you to use Scala as Better Java for the time being
Hello World, a cross-platform GUI app with Groovy running on the Java platform
[Ruby] Define the hierarchy at the same time as Hash initialization with tap method
[Deep Learning from scratch] in Java 1. For the time being, differentiation and partial differentiation