1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ruby初心者の自分がmruby触ってみた(環境構築)

Last updated at Posted at 2021-03-15

こんにちは、smpeotnです。
今回は、mruby3.0がリリースされたとういことなので、ちょろっと触ってみようかなと思います。

mrubyってなんでしょう?

正直、自分も今日まで知らなかった。 朝、mruby3.0リリースされたってよって記事を見て初めてこうゆうものがあるんだと知りました。
  • 省メモリ
  • 言語仕様が小さい
  • 軽量なRuby言語処理系言語
  • LuaのようにかんたんにC・C++に組み込むことができる

なるほど。。。。使い方によっては色々できそうだ!

環境構築してみる

DockerでRuby環境をまずは:こうゆうときDockerって便利ですよね
Dockerfile
FROM ruby:2.5
RUN mkdir /data
docker-compose.yml
version: '3'
services:
  ruby:
    build: .
    tty: true #コンテナを起動しっぱなしにする
    volumes:
      - .:/data
# 起動 !!
% docker-compose up -d

https://github.com/mruby/mruby/blob/master/doc/guides/compile.md
ここに書かれてる内容を確認して必要なミドルウェアのインストールとかする

  • ruby 2.5
  • gcc
  • ar
% docker-compose exec ruby /bin/bash
root@4c1ad477fc54:/# which gcc
/usr/bin/gcc
root@4c1ad477fc54:/# which ar
/usr/bin/ar
root@4c1ad477fc54:/# which ruby
/usr/local/bin/ruby

root@4c1ad477fc54:/# gcc --version
gcc (Debian 8.3.0-6) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

root@4c1ad477fc54:/# ar --version
GNU ar (GNU Binutils for Debian) 2.31.1
Copyright (C) 2018 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) any later version.
This program has absolutely no warranty.

root@4c1ad477fc54:/# ruby --version
ruby 2.5.8p224 (2020-03-31 revision 67882) [x86_64-linux]

Build Compile.

root@4c1ad477fc54:/data/data/mruby-3.0.0# rake -v
.....
root@4c1ad477fc54:/data/data/mruby-3.0.0# ls -al ./bin/
total 23296
drwxr-xr-x  7 root root     224 Mar 12 15:26 .
drwxrwxr-x 38 root root    1216 Mar 12 15:25 ..
-rwxr-xr-x  1 root root 5990688 Mar 12 15:25 mirb
-rwxr-xr-x  1 root root 3773904 Mar 12 15:25 mrbc
-rwxr-xr-x  1 root root 5970456 Mar 12 15:26 mruby
-rwxr-xr-x  1 root root    1031 Mar 12 15:25 mruby-config
-rwxr-xr-x  1 root root 6040224 Mar 12 15:26 mruby-strip

root@4c1ad477fc54:/data/data/mruby-3.0.0# ./bin/mruby --version
mruby 3.0.0 (2021-03-05)

# パス通す
root@4c1ad477fc54:~# export PATH=/data/data/mruby-3.0.0/bin:$PATH

root@4c1ad477fc54:~# mruby --version
mruby 3.0.0 (2021-03-05)

これはDockerfileに書けばいいよね...

Dockerfile
FROM ruby:2.5

RUN mkdir /data
RUN apt-get update && apt-get install -y vim
RUN apt-get install -y wget

RUN mkdir -p /usr/src/mruby
WORKDIR /usr/src/mruby
RUN wget https://github.com/mruby/mruby/archive/3.0.0.zip -O mruby-3.0.0.zip && \
unzip mruby-3.0.0.zip && \
cd ./mruby-3.0.0 && \
rake -v

ENV PATH $PATH:/usr/src/mruby/mruby-3.0.0/bin
WORKDIR /usr/src/mruby/mruby-3.0.0

Hello World.(https://github.com/mruby/mruby/wiki/Hello-World)

hello.c
#include <stdio.h>
#include <mruby.h>
#include <mruby/compile.h>

int
main(void)
{
  mrb_state *mrb = mrb_open();
  if (!mrb) { /* handle error */ }
  puts("Executing Ruby code from C!");
  mrb_load_string(mrb, "p 'hello world on mruby! this is ruby code.'");
  mrb_close(mrb);
  return 0;
}
root@ddd2a0d85a35:/usr/src/mruby/mruby-3.0.0# gcc -Iinclude hello.c build/host/lib/libmruby.a -lm -o hello.out
root@ddd2a0d85a35:/usr/src/mruby/mruby-3.0.0# ./hello.out
Executing Ruby code from C!
"hello world on mruby! this is ruby code."

できた!!!!!
Docker簡単。ここまでできれば色々検証できるで

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?