RubyConf TH 2023

Kickboxer vs. Ruby - The State of MRuby, JRuby and CRuby

A talk from RubyConfTH 2023, held in Bangkok, Thailand on October 6-7, 2023.
Find out more and register for updates for our next conference at https://rubyconfth.com/

RubyConf TH 2023

00:00:08.000 Ladies and gentlemen, welcome to the ultimate coding showdown!
00:00:10.200 In the right corner, we have Michael Milewski, the people-centric developer. Michael thrives on the human side of software development. He loves pairing with others, especially newcomers to coding, and excels at driving software with his team.
00:00:17.160 When he's not coding, he enjoys camping with his kids. And in the fresco corner, hailing from Melbourne, Australia, we have Selena Small, a force to be reckoned with! Selena combines her impressive skills as a full-stack lead engineer with her passion for test-driven development. She is also a kickboxing professional.
00:00:31.000 Get ready for a clash of technical prowess and mentoring abilities! Will Selena's kickboxing spirit translate into knockout performances in Ruby, or will Michael's people-centric approach lead him to victory? Stay tuned for this epic showdown!
00:01:41.320 Hey everyone, I'm Selena and this is my aspiring partner, Michael.
00:01:43.439 We're here today to talk about our journey with Ruby and kickboxing. That's right! Today, we'll be exploring the state of MRuby, JRuby, and CRuby.
00:01:54.719 Before we get into it, let's play a quick game. Who likes games? Also, it'll help us catch our breath! Who likes giveaways?
00:02:08.200 All right! We have a prize to give away! All you have to do is post a tweet with the hashtag #KickboxerVsRuby. In about 20 minutes, the best tweet will win a prize live on stage!
00:02:26.280 So let's start with some background on our talk. I’m sure the audience is wondering, what exactly is kickboxing in this context? Is it some kind of new tool or library? Good thought! You're probably right.
00:02:51.680 Selena is a kickboxer, and many of you might remember that a few years ago, a worldwide pandemic broke out. In Melbourne, where we are from, we endured harsh lockdowns, which meant Selena couldn't train with her usual sparring partners.
00:03:10.580 So she came up with the crazy idea to build a sparring robot using Ruby! A key part of training for combat sports is all about improving your footwork. I thought I could take a drill that I've been using at the gym with my coach and create a robot that would strike the athlete with pool noodles.
00:03:27.640 The athlete would have to use their footwork to swiftly evade the strikes. I reached out to my friend Michael, who loves collaborating on projects and knows a bit about hardware, too.
00:03:54.640 We brainstormed the various moving parts of the project and discussed potential blockers. Our first thought was to use an actuator as the hardware device that Selena would be training with. For this, we planned to use a motor or servo along with a simple microcontroller device like an Arduino.
00:04:10.640 We also wanted to control it remotely, so we needed Wi-Fi for network connectivity. An ESP32 would work perfectly, as it's basically an Arduino with Wi-Fi capabilities. But the question was, can we run it with Ruby? Quite likely, using MRuby, a lightweight implementation of Ruby designed specifically for embedded systems.
00:04:42.640 It's ideal for devices with limited resources. To control it remotely, we would need some kind of server for the actuator to connect to. But will that run Ruby? Yes, we can use CRuby, also known as Ruby MRI, for this part of the project.
00:05:09.680 The server will receive data from the actuator and communicate with a mobile device that I will be using to control it. For the mobile client, we can choose between Android or iPhone. However, since Android is a Java platform, we could inevitably use JRuby, which implements about 98% of Ruby.
00:05:30.680 So if we can write it in Ruby, it should run on JRuby. That means I'll be swinging a pool noodle to practice my footwork!
00:05:51.680 It's going to be such a cool project! I can't wait to see it come together. Especially since I'll be on the other end throwing the attacks at you with my mobile app.
00:06:10.840 Now that everyone understands the plan, let’s get started with the actuator. Our first thought here was MRuby. Last year, during RubyConf Thailand, we heard Yugi Yoku talk about building a Wordle game using MRuby that ran off a Sega Mega Drive.
00:06:25.680 Back in 2020 at RubyConf in Australia, Paul and TH presented a build of a voice-based smart home using Sinatra and MRuby. Unfortunately, Wi-Fi support in PicoRuby and RP240 was missing at the time.
00:06:42.760 So we had to give that one a miss. But there are so many possibilities! We came across an MRuby ESP32 application template, which sounded like a quick start.
00:07:01.840 However, we soon found out that it wasn't as simple as we thought. We started out with the assumption that we could just install MRuby, but it turned out to be much more complicated.
00:07:26.560 We had to go through multiple steps: copying C project files, MRuby source files, and MRuby components. Plus, we had to use the ESP-IDF, which is the Espressif IoT Development Framework, to compile the project.
00:07:52.760 After all that, we were finally able to push our code to the ESP32. What a headache, right? Maybe we could contribute to PicoRuby and get that Wi-Fi support!
00:08:01.080 That way, we could spend more time coding Ruby, and less time compiling C.
00:08:11.080 Now, let’s take a look at a demo!
00:08:26.080 As we plug in this battery, we should see a flashing light. But that’s not Ruby! Let’s see if we have an MRuby version.
00:08:44.680 This is an MRuby implementation, and it’s much brighter. Now, everyone might be interested in the story, but can we take a look at some code?
00:09:11.280 On the left, we have straightforward ESP32 C code. We define a pin, set it as an output, and in an infinite loop, we set the output to high for one second and low for one second.
00:09:24.440 Meanwhile, in the MRuby implementation, we could accomplish the same operation with much less code. However, the MRuby code was quite verbose.
00:09:43.560 It was hidden in other classes, making it slightly complicated. But it was sufficient to get a light flashing. We need to do a bit more to get it connected to a server so we can remotely control it.
00:10:00.680 Who wants to see another demo? So, let's plug this in and fire up a client. This client will connect to our network to register the devices.
00:10:19.680 Now, we should be able to press enter, and one of the actuators will start flashing. This is the actuated device. Now, I can switch to the next actuator, and it should flash as well.
00:10:39.680 Great! We now have connectivity from our devices to the network.
00:10:42.560 Just in case you didn’t see that, please clap!
00:11:10.080 Now, let's take a closer look at the code. On the right, we have the MRuby code, which is concise, taking up only one page. On the left is the ESP32 code, which spans four pages!
00:11:22.640 So much more code for the same functionality! In MRuby, we can work in a way that feels familiar to Ruby developers, while the C code is much more cumbersome.
00:11:44.640 Let’s review what's needed to connect to Wi-Fi and check if a connection exists. In C code, we must manually implement this, whereas MRuby simplifies the process.
00:12:02.720 On the left in C code, there's a lot happening—copying data files to buffers just to concatenate strings. But you can see the MRuby code is much more accessible.
00:12:39.760 Every project needs a server, and we all know how to use CRuby and servers like Rails or Roda. We assumed this would be straightforward, especially when dealing with IoT devices.
00:13:03.680 However, these devices require minimal communication, and that's where the MQTT protocol comes in. MQTT stands for Message Queuing Telemetry Transport, allowing messages to pass between various systems.
00:13:29.760 So, we looked for a broker and found Eclipse Mosquitto, which worked perfectly! We didn’t even need C or Ruby code for this, and it simplified the integration.
00:13:50.160 Next, we tackled the mobile client. We started with the assumption that creating a mobile application with JRuby would be quick and easy.
00:14:22.240 We thought it would be a straightforward process: write some code, hit run, and voilà! But it turned out to be more complicated than expected.
00:14:43.040 We began writing the client in plain old CRuby, and surprisingly, it worked! It listened to the MQTT server and allowed us to activate actuators remotely.
00:15:01.680 This was the demo showcasing the CRuby implementation, proving that it could activate the actuators. But our goal was to build it using JRuby, since that's what this presentation is about.
00:15:24.240 We hoped converting the code to JRuby would work seamlessly, with a few adjustments to the libraries we used.
00:15:48.160 Now, let's highlight the functionality of JRuby, with a working code demo! The client on my laptop shows compatibility with JRuby, running on our actuators.
00:16:01.760 If we activate it, both the left and right actuators light up! However, we encountered some issues with terminal commands.
00:16:26.560 Despite that, we found that JRuby could work effectively. However, the client was running on a laptop, which is not what we initially aimed for with mobile devices.
00:16:50.160 Next, we learned about a solution called Roboto, recommended to us at the conference last year. We were excited to try it, but the website was down when we checked.
00:17:15.880 Nonetheless, we reached out to one of the contributors, Charles, and soon enough, the site was back up and running!
00:17:34.160 We supported our mobile client and pushed through the next challenges to get to the demo. I'm sure you all want to see the robot in action!
00:17:54.000 This is an actual Android device where the kickboxer application connects to the actuators. We press the button to start it up.
00:18:10.800 Right now the app recognizes two actuators! The light shows that they've successfully registered. But of course, we want it life-size!
00:18:30.560 At this point, I'm going to get into the ring with the robot. Let's go!
00:19:06.880 Please clap!
00:19:26.560 Ladies and gentlemen, we have a winner in this thrilling coding showdown! After all, the Ruby programming language emerged victorious!
00:19:46.760 Give it up for Michael and Selena!
00:20:04.240 As Selena prepares for her next session, she's about to find out the winning tweet so we can give away some prizes.
00:20:21.240 Whoever is the next speaker can bring their laptop up for the presentation. So many good tweets! This one right here by T.Raj wins!
00:20:46.760 He captured the knockout shot of Michael lying on the ground at the beginning. Congratulations!
00:21:04.760 Winner!