RubyConf 2015

Creando máquinas de estados en aplicaciones Ruby con Ragel

Ian Duggan  · 

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

- Okay, guess we'll get started now. Welcome everyone, this is Stately State Machines with Ragel. Thanks for coming to my talk, I guess there was a couple to choose from, so I appreciate that. I'm a little jet-lagged, so if I seem like I don't know what I'm talking about, that's probably why.

Who here has heard of Ragel before or worked with it, just so I know (mumbles) online, okay. Okay, all right, we'll just get started. So the goals for this talk are to convince you that Ragel's worth trying if you haven't used it before. And to give you some intuition about how it works, and to show you how to set it up so that you can kinda do some basic parsing with it.

A little bit about me, my name is Ian Duggan. I'm into lots of different things, I play hockey, several times a week. This is Stormy the Pig, where I come from, North Carolina, Pigs mean hockey. I play guitar. I play banjo, mandolin, ukulele, I have a fiddle that's gathering dust, and my cats actually appreciate that.

Sometimes I fly, I've got a Cessna 120 that I fly out of Hayward, California. It's got a whopping 85 horsepower, but it's fun to putt around the Bay and the Valley with that. I love my cats, a couple, you know, gotta have cat pictures in a presentation, so here they are.

This is Purrington, we call him Boofus. This is Minnie, she's a goofball, she likes to wear outfits. She doesn't care, she thinks they're fun. This cat likes being cozy, this one's a dopey, Chill. Minnie likes to sit in strange places. So I'm a software guy, I code things.

I code the internets and the googles, I'm a recovering technology entrepreneur, been in and out of startup institutions for many years now. My current status is that I work for Twitch. We do online streaming social video games. Obviously we're hiring, we have lots of Ruby, the whole front end is Ruby on Rails, the backend is getting redeveloped into Go, for a lot of our services, 'cause we need the performance.

I've been using Ruby for quite some time now, I started on 1. 6 days, a friend of mine gave me a 1. 6 book and I just kinda played around with it, thought it was a pretty cool language. So I don't know when the transition happened, but I've just kinda stuck with it since.

But enough about me, this talk is about Ragel. Ragel is this really cool tool, and it's used in a lot of sort of high-performance techs parsing areas, and you know, some people seem to have discovered it, and are using it for stuff like that, and there's a lot of other use cases where people are kinda flooding with regular expressions, where I think Ragel could be really helpful.

It's got a lot of features that allow you to do some kind of really cool tricks with it. And if you don't have it in your bat-belt, then I'm hoping that you'll add it today. As I mentioned, there are many projects that use Ragel. Mongrel, Unicorn, and Puma, are the common ones that people know about most, Zed Shaw I think kinda popularized it about 10 years ago when he rewrote the Mongrel http protocol parsing API with it.

There's a really cool gem called Whitequark, which actually parses Ruby using Ragel. The mail gem RedCloth, Hpricot, Gherkin, they all kinda make use of it. So just really quickly what does Ragel look like? We'll jump into it, this is DSL, you can kinda define some actions, you've got some.

. . Ways of converting different terms into other terms. So that's kinda what it looks like, and we'll get into the details very shortly. So most people are familiar with regular expressions. Regular expressions are easy, for the most part. People understand kind of the basic stuff that we all understand.

Matching, grouping, alternation. Matching zero, one over r, end times, end times, et cetera. And Ruby has really great tools for regular expressions, 'cause it has a heritage from Pearl, Sed, and Awk, which relied heavily on those. So you could do fancy things like match stuff, and in a block, match some more stuff, and in a block, replace something else, free regular expressions, so you can get pretty fancy.

But at some point, they can become irregular expressions. So if you've ever opened up a file and stared at something like this, then Ragel might be something you would consider. They have their place, but there are better tools. Sometimes you want more control, I posit that this means you want some sort of automaton.

So just a little bit about automatons. Like finite automata have, basically, these devices they have states and transitions, many people have probably seen these, you've got circles (mumbles) and some input comes in, causing a transition to another state.

There are two kind of basic types, there's a deterministic finite automata and non-deterministic finite automata. It's just kind of an academic distinction. Deterministic means it could just be of one state at a time. Non-deterministic means you can be simultaneously in two different states.

However, you could map those multiple states as superstates of the single states and convert that into a deterministic finite automata. So in academia they talk about this equivalence of regular expressions, NFAs, and DFAs. So all this stuff that you're doing with crazy, regular expressions you can kinda do with these other tools.

So you should choose the tool that kinda makes the job easiest for you at that point in time. Excuse me. So as a superset, these are all state machines. State machines are an important tool in computer programming, Ragel is a wonderful tool for creating them.

And state machines are everywhere. They're in your stoplight, you have green, the timer goes to yellow, goes to red, goes to green, goes to yellow, goes to red, goes to green, around and around forever. They run your CPU. If you imagine your computer, like every single bit that 's in it, as kind of a definition of a state, then every piece of input causes a transition to another state, your computer is one gigantic state machine.

There's some kinda non-state machine-ish stuff that happens on the edge when inputs (mumbles), and stuff but for the most part, you can think about it like that. It's a big honkin' state machine. And there's other examples everywhere. So like, watches, vending machines, I've mentioned traffic lights already.

Barcode scanners, pretty much any piece of machinery you see these days that isn't analogue. Has any kind of computing device in it, it's gonna be a state machine. So I think they are the cat's meow, they are great for many reasons. They're simple to understand, and there's a great deal of research around finite automaton state machines.

With the right approach they can also produce code that is faster, easier to maintain, and correct, more correct, and thus more secure. They're very specific about what inputs are accepted and what aren't, and it's, it's harder to get it to parse something incorrectly than it is for it to admit something that it doesn't parse.

Which can be very useful. If you're still not convinced, we should probably just spend some time looking at what they are. So let's go over some vocabulary. So state machines, they all have something called a start state, usually designated as zero, this is as it sounds, like the first state that the state machine starts at.

[ ... ]

Nota: se han omitido las otras 3.584 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.