The Rubyist's Illustrated Rust Adventure Survival Guide

0 0

[Music] so the full title of this talk which does not actually fit on most CFP submission forms is the illustrated adventure survival guide for Noura stations and natives of rubyville with apologies to why the lucky stiff how many of you are familiar with why the lucky stiffs work awesome that's so great that's the most people I've seen in a room and I've given this talk like 10 times well okay so if you are if you are familiar which many of you are you'll recognize a few things in this talk if you're not I would strongly recommend looking from oppositus he was a Rubeus who did a lot of really interesting work that was a big reason for me getting into programming and this whole talk is kind of in well half the talk is an homage to wise poignant guide to ruby which i think you can find online somewhere for free if you want to check it out anyway welcome I really have to commend you it took a lot of courage to sign up for this mission this adventure we're about to embark on is not for the weak or the faint of heart so if any of you are pregnant or nursing or suffer from any kind of heart condition or if your doctor has advised against the eating of spicy foods I ask that you consider consulting your physician spiritual adviser psychic medium or nosy next-door neighbor before accompanying me on this journey while we wait to board the ship some of you might want to know a little bit about me your guide before moving forward with this seafaring journey and I get it I understand I don't look quite like a seasoned ship's captain and you are right I'm not I'm Liz I used to be a cartoonist I drew comic books I went to art school I drew a free a few graphic novels a few years ago I learned to code I went to the Flatiron school in New York and I started working in web development nowadays I work at Tilda in Portland Oregon mainly I work on our product skylight which is an application that helps developers working in rails or other Ruby based frameworks to optimize their own apps we also use rust which is a big part of why I started learning rust in the first place so before we get on the ship I'll show you a map of where we'll be going right now our ship is docked at the port of JavaScript just off the coast of rubyville we'll be sailing the seas of chunky bacon and should be landing as a cargo bay of rough land eeeh in no time so let's get onboard take your seats no standing no eating or drinking but most importantly no staring at the ship captains aye he's really sensitive about it so say goodbye to your loved ones and off we go sailing sailing over is abounding name this is a song about sailing I don't know the rest of the word if you all look out your windows to the west you'll notice a beautiful sight some foliage that's native to rubyville and abstract syntax trees its nodes are particularly lovely this time of year you're probably used to seeing these that you're from removal they tend to sprout any time some code gets thrown into the interpreter just before it gets turned into byte code so the Ruby virtual machine can run it as you might already know Ruby is an interpreted language so this is more or less what you're used to if you're Rubeus but in Westland do we have to remember to compile our code before we can run it otherwise it won't work so when we get there just remember to keep raises cargo build and cargo run they will come in handy when we reach the shore and tart and start trying to chat up the locals if you try to just run your code directly like you did in Ruby though they won't know what you're talking about I won't forget to mention on the way out you'll notice a big pile of mains don't forget to take one you'll need to put all the code that gets run for your program inside of main unless you're building a library but we're not there just yet so very important all right everybody off the ship here we are welcome to russ land yeah let's check out the town remember things move a lot faster here so be home check it out it's a second heat proof of let's go inside you're probably used to not giving much thought to memory back in Ruby though before I came to Russell andia I had heard of the sack in the heap but I knew it is something I decided to his memory but I didn't really understand it so watch how things work at the bar people come in and they give their programs to the bartender and she compiles and runs of them someone wants a fancy whiskey and a cheap whiskey the good stuff is fancy so we put that in a box and store it on the heap because we want to we want it to be able to stick out stick around for a while even though it's high up and it's a bit slower to get to so most things in rust are stored on the stack unless you specify otherwise which is why we had to put the fancy whiskey in a box in order to store it on a heap so when we run this program the good whiskey gets a spot on the heap at the far far end of our available memory and we put a pointer on the stack that points to that spot I'll meet next on the stack we put the cheap stuff in this case it is old grain grandad which is a very terrible bourbon if you didn't know the cheap stuff get served up first because it's on top and then we have the pointer to the fancy stuff which the bartender has to go way way way up to the top of the heap to get which she does all that sea travel made me hungry I think let's check out one of the local eateries I've heard good things about this one it's called cafe destruct Oh sir I can't quite read this menu and we have to seeing things like this there's a defined class and the instance method so that cos they define with a simple def and they end with the word end instead I feel these strange new things like structs and impulses what's this all about huh we're so sorry madam I know this isn't something you're used to back in Ruby bill but we here in Northland yeah we uh we have no class no class I can't believe it this is terrible how are we going to get along without class it's the one thing separating us from the animal don't worry don't worry at all you can still get what you need here here here's the chunky Bay construct this is where we define all the attributes we expect out of it like flavor chalkiness price etc so then I can just do something like this and I'll be all set no no my good lady for that you'll need to write and improve that's an implementation of chunky bacon so if you want a new instance of chunky bacon you'll have to write a new method yourself it doesn't just happen automatically well then very good I'll have a have a salad and now a word from our sponsors hey MEK me yeah you there you wanna try some rust you mean the iron oxide produced as a result of a redox reaction of iron and oxygen in the presence of water or air moisture what have you been reading Wikipedia no not that restful rust lying my friend oh yeah that cool means systems programming language all the kids at school are talking about the very same well what about it do you like abstraction without overhead I don't know how about concurrency without data races I think what about memory safety without garbage collection that sounds good I really don't do a wall ruff welcome to mutability Lake it's a lovely day and so many of us distinguish towns people are out sailing their toy ships some are fancier than others some are mutable some aren't this one here is a nice one this one's not mutable so we can't change anything about it but I can pick it up and show it to you look how nice but if we try to change anything about it hey you can't do that ah the compiler is yelling at us so we know we can't change this boat it's immutable let's try another one let's look back what are the ships are under like cool this one's definitely mutable it has a little flag that says me let's just make a few changes before we return it let's add some wheels to this boat perfect compiler what do you think no what's the matter compiler you put wheels on a boat it's not a boat anymore it's a car you said you would return a boat you must return a boat as is clearly illustrated here the boat stroke does not include anything about wheels ok ok I'll take the wheels off I look pretty cool though let's look for another boat in the late hey that one looks pretty cool you like hey that boat doesn't belong to you oh I'm sorry compiler to lure this boat I do so sorry can I borrow it I was hoping to play with it in my bathtub well will you bring it back of course go right ahead don't forget this ampersand so everyone knows you're just borrowing it and now a musical montage hey thanks for returning my boat compiler does everything look good to you know you added a tugboat that's not okay but compiler these boats are all vectors of strings all I did was push a tugboat onto it a tugboat is a string the tugboat turn in yours and neither is a boat yeah that talk about is mine well what if I just boom punch the tugboat guy in the face and hit him with a remove looks like no one owns this plug boat now you want to talk about on your boat sir sure let's do boat that push and passed in the tugboat now you have one and now another word from our sponsors hey kid what did you get all that information about ownership and borrowing huh that's how we get a lot of the cool stuff I was telling you about before you mean like when you were yelling memorize thanks even a card was cocaine at me yes you got it I only know Ruby though so these aren't really problems I've had to deal with well let me tell you if you were C programmer you'd be really excited okay you bet it's okay can you just teach me something about rust so the kids at school will finally think I'm cool sure thing kids let's try a less convoluted metaphor what's this hello again travelers have you seen our Steam library it's pretty great you can borrow just about anything as long as you return it they even have this great big pile of books over there that don't throw in to anyone you can just take them if you want and then they're yours every so often people will come by with donations of books they don't need anymore it's great if you want to borrow a book you just need your ampersand if you try to change the book while you're borrowing it the compiler will yell at you and your code won't compile however if you see something you like in the free pile you can just take it and do whatever you want with it it's your your there in the back you had a question yes what about if the book is immutable reference great question friend well you might very well be borrowing something that's mutable like our collection of coloring books here you can continue coloring in them while you have them and you can return them altered only one person can have one out at a time though we also have some exquisite corpse boats that are pretty cool every time someone borrows one of these they add a little bit of it themselves before bringing it back I want to borrow the exquisite corpse book me too I want to borrow it to know no more than one mutable reference at a time you can have it now but you have to wait until the first person is done before you can borrow it oh man no complaining do you want a day to race do you know ok then so you might have noticed how clean and beautiful Russell India is and yet you might have also noticed there are no garbage cans anywhere it's actually because of that system of borrowing and ownership that rosslyn do is able to do without garbage collection it's a big part of what makes everything so fast and safe here but isn't it annoying having the compiler yelling you Adil yelling at you all the time hey now don't judge the compiler look you hurt his feelings he's not such a bad guy he's just making sure everything we do is good before we can run it the compiler is our friend he just wants the best for us sure his advice might be a little hard to understand at times but once you get to know him he's really a good guy I promise and now another word from our sponsors tonight on WR St it's the true story of how one statically typed programming language risked it all to figure out how to handle it when things go wrong Being and Nothingness and error handling based on a true story if everything goes right the reactor will cool down and the city will be saved but if something goes wrong if this whole thing goes south I guess I'll just do nothing I mean it seems like it's time to return an option right I mean the compiler feels fine about it right looking good meanwhile across town the grocery store doesn't have the mustard I like Red Alert red alert everyone pay attention to me shut everything down I need my Dijon compiler you agree don't you compiler looking good public service announcement just because the code compiles doesn't mean it's something you should do results for when something could go terribly wrong and you need to throw an error option for when it's okay to just do nothing with it learn it love it hey welcome back just in time for the last boat back to rubyville I sincerely hope you enjoyed your stay and that you visit again soon if you want to stay a little longer there are some very nice boxes at the heap otherwise the boat is ready to board on your way back we do have some very nice reading material if you're interested in learning more about rust I strongly recommend starting with rust by example this is an online book that will lead you by the hand step-by-step through many examples explaining everything along the way after that you can look at the official official rust reference documentation which is very helpful if you'd like to try your hand at some rust of your very own from scratch there are many excellent exercises available at exorcism if you have questions there's the rust Reddit and the user forums or you can chat on one of the many channels on IRC in preparation for the original version of this talk at restaurants last year with some help from my boss I've developed a playable text-based adventure game in both Ruby and rock this way you can check out Ruby and Rosco that do similar things side by side I've written a very few very little ones over the years usually when I'm trying to learn a new language or if I'm playing with a new idea some of you may never have played one but it's basically a game where everything is text in my case the whole game is played in your terminal like it's the 80s so the way it works is you get a prompt usually a sentence or two followed by a question like you are in a dark forest you see a light in the north you have a map what do you do and then you type something like look at Matt and the computer responds with something else and you type something else the computer responds to that so on and so forth until your character dies so the game is over I think nowadays games like this are also called interactive fiction let's take a quick look at the actual game I wrote to give you a better idea of what I'm talking about let's see how this goes okay whoa so ruby means gonna be okay this up quite a bit what is your name my name is Liz what would you like to do and look around this room contains a rare and glorious unicorn it's amazing this room contains a jar of uniform for its unicorn doctors here too what now talk hi I'm a unicorn doctor it's pretty cool I have a vial of unicorn blood you want it what now Oh see take a vial of unicorn blood who doesn't want a vial of unicorn blood okay I wrote it wrong takes vial of unicorn blood maybe vial of unicorn blood has been added to your inventory and removed from the doctors in front okay I want to use the vial of unicorn blood I want to know what this does uh it's made me invincible it's amazing so great okay I want to get out of here so oh no it's go back and hopefully this word [Music] okay so that's the game but how did I actually build it before writing a single line of code I had to think about the architecture of the game there are a million different ways I could find a structure it in the end I wanted something super simple so I went with a really basic loop I continues to ask what the player wants to do until a particular condition is met at which point the game ends so in this example which I stressed is not real code just an example of the structure I was using we start by defining playing is true and while playing is true the game asks what do you want to do next then it parses whatever the subsequent user input is and it performs an action based on that input like if you just says move north then the players position is changed and maybe we output something like you have moved north we don't egg the loop unless the user dies or the game ends at which point playing is redefined as false so what does this look like in Ruby or rust in Ruby there's a lot going on before we get to the play method but that's where the loop is so that's what I'm showing you this is a method on the game class which is initialized with a player a map and array of rooms and with the value of playing as true so as long as playing remains true the game will keep asking what now then I'll parse what have you the types and respond to that so this is this example is ignoring a lot of other code I wrote to support this so it doesn't fit on the screen much like the Ruby example but for the sake of simplicity here you can see we are creating a player a map in a game and you can see that while game dot playing game play pretty close to what I Ellis traded earlier what you can't see though is that unlike Ruby I had to write my own new method because Russ doesn't automatically give you anything like Ruby's initialize and isn't object-oriented by nature the way Ruby is so there are no classes per se otherwise it seems pretty similar to the Ruby version just a little more code which is fine so what differences did I notice in writing the game with Ruby I felt like I could just write whatever I wanted make something work quickly and then reorganize the code as I went it's really easy to have a flow like you're just writing your ideas in your journal and you're making things up as you go along but the downside of this is that it's really easy to get caught up in that flow and forget to check things out as you go I forget to test forget to run every single part of the program you just wrote which I did many I periodically run the program and things which is breaks without adding a whole bunch of tests after the fact which isn't great I didn't have a lot of confidence that something wouldn't just unaffected like unexpectedly fall apart at runtime well because I got caught up right in the game and I forgot to check things out as I went along my rust on the other hand is kind of the reverse experience since rust needs to be compiled before you run it the compiler literally won't let you run the program until you fix all the stuff it doesn't like not coincidentally I have not run into any of these like pesky random bugs in the rough version of the game probably cuz the compiler caught them before they would become a problem this can be super frustrating though when you're first learning or us because it can be so satisfying just to see something working and you could be working for hours and seeing errors like this over and over and it can easily become really disheartening if you're new to it um yeah it's yeah Russ is good this might be it seemed like a weird simile the best thing I can think of to compare Ruby and Russ in terms of my personal experience with the two is like there are two different parenting styles this might seem like an odd comparison but bear with me Ruby is like the hippie parents to let their kids do whatever they want they don't put a whole lot of restrictions on them because they're like you'll figure it out man it's cool you'll be okay in the end I trust you the downside of course is the kids will hit all these bumps in the road along the Ray way because no one told them like you shouldn't you know go walk into traffic or whatever ruff is like those parents who are constantly trying to kids what's best for them they're a little bit strict it's like that bad bad but they're just always giving this unsolicited advice based on their own experience and like maybe it turns out their advice was right but it's like definitely annoying to be hearing it all the time and just like parents you can't really say one way of doing things is better or worse than another what people's have their pros and cons depending on what you're trying to do and how you prefer to work I'm sure some of those more experienced could give a more in depth analysis but this is my opinion based on my limited experience just reading a simple text game so if you're thinking of wearing rust or Ruby I'm sure you're all Ruby people anyway or both I say go for it you can check out the game I built at github comm slash tilde IO slash learning - ruff both versions are playable and both have kind of filler stories in them the Ruby versions filler game is a little more fun though if you can only play one of them thank you for having me I have been live i'm on twitter at underscore l Bailey that's it the end [Applause] [Applause]