Slide 1

Slide 1 text

Porting ruby gem to mgem Fukuoka mruby kaigi େ૔խ࢙ (OKURA Masafumi), 2022-10-15

Slide 2

Slide 2 text

mruby

Slide 3

Slide 3 text

Why mruby?

Slide 4

Slide 4 text

Because it’s Ruby!

Slide 5

Slide 5 text

Why Ruby?

Slide 6

Slide 6 text

Because there’re lots of gems!

Slide 7

Slide 7 text

Are there lots of gems in mruby world?

Slide 8

Slide 8 text

NO😭

Slide 9

Slide 9 text

Why?

Slide 10

Slide 10 text

Me • Name: OKURA Masafumi • Job: Freelance software developer • Expertise: Ruby, Ruby on Rails, Neovim • Organizer of: Kaigi on Rails, Grow.rb, Entaku.rb, Rubygems Code Reading Meetup • Author of: Several gems including Alba (with more than 500 stars)

Slide 11

Slide 11 text

mruby 3.1.0

Slide 12

Slide 12 text

It’s a lot improved!

Slide 13

Slide 13 text

Many “No”s

Slide 14

Slide 14 text

“No”s of mruby • There’s no `require` and `require_relative` • There IS a mgem for `require`, but not for `require_relative` • There’s no familiar testing frameworks (MiniTest and RSpec) and tools • There IS a simple test framework • There’s no `public` and `private` • It’s actually fi ne…

Slide 15

Slide 15 text

require and require_relative • Most of the CRuby gems depend heavily on `require` • In mruby, fi les are loaded by alphabetical order • If we name fi les as usual, class “A” cannot depend on class “B” (not loaded yet) • If the gem depends on `require_relative` that doesn’t work • We need some ways to avoid `require`

Slide 16

Slide 16 text

Testing • Most of the CRuby gems use either MiniTest or RSpec for testing, both of which don’t work with mruby • There’s https://github.com/iij/mruby-mtest so if it uses MiniTest it might be able to convert • There’s no familiar testing tools such as FactoryBot and Faker • There’s https://github.com/okuramasafumi/mruby-factory for simple factory usecases, but not one for fake data

Slide 17

Slide 17 text

But I want more mgems!

Slide 18

Slide 18 text

Solutions

Slide 19

Slide 19 text

Convert code as text

Slide 20

Slide 20 text

How to convert gem to mgem • Start from “main” fi le, and if there’s `require` or `require_relative`, replace it with the content of the fi le • Do this process recursively • If there’s MiniTest test cases, copy each test fi les and put them into one fi le, replacing `MiniTest::Test` with `MTest::Unit::TestCase` • The fi nal result would be two large fi les: One is for production code and the other is for test code

Slide 21

Slide 21 text

Tool

Slide 22

Slide 22 text

https://github.com/ okuramasafumi/ gem_to_mgem

Slide 23

Slide 23 text

What it does • Resolve all `require_relative` into the contents of fi le • It does nothing about `require` • If `require_relative` is in a method, move it to the head of the fi le and resolve • Collect all MiniTest test code and put them into one test fi le

Slide 24

Slide 24 text

Under development

Slide 25

Slide 25 text

Other missing things

Slide 26

Slide 26 text

How to deal with other missing things • public and private: simply delete them including `private_constant` • `public_send` should be replaced with `send` • ENV: simply delete it • External libraries (dependencies): Convert them into mgem recursively • It’s hard and when it hits C extension, it’s over! • We need to do these manually

Slide 27

Slide 27 text

What about testing tools?

Slide 28

Slide 28 text

Forget and delete :)

Slide 29

Slide 29 text

Seriously • We need to “bootstrap” by converting Faker and other testing gems into mgems • We can implement our own minimal Faker gem

Slide 30

Slide 30 text

Implement from scratch

Slide 31

Slide 31 text

https://github.com/ okuramasafumi/mruby- factory

Slide 32

Slide 32 text

mruby-factory • My own re-implementation of FactoryBot • No tools are used, it’s just a rewrite of FactoryBot from scratch • Probably 30~40% of the features are covered • It might be enough, might not be enough

Slide 33

Slide 33 text

Conclusion

Slide 34

Slide 34 text

Way to more mgem • Use https://github.com/okuramasafumi/gem_to_mgem to do some conversion for the gem you want to make into mgem • On other parts, we have to tweak manually • Sometimes it’s simply impossible • We can also write our own version from scratch

Slide 35

Slide 35 text

Happy mrubying!