 Hello, okay Thanks for coming my name is John Lamb and and I work for that company over there At least I do now and I'm here to kind of tell you a little bit of a story to start off about You know the kind of whole process for getting To where I am today At the last RubyConf in October I announced that I was coming to Microsoft to To work on dynamic languages and that's still true But I thought I would give you a few pictures to kind of show you a little bit more about the actual process itself So that's where I used to live in Toronto right with a big giant sold sign sitting in front of my house If you've ever had to move a house without much crap in it You'll see that at some point it looks something like that Right, that's my dog kind of really confused down there about what's going on with all of this stuff Pardon me. No, that's bubble wrap. No, we didn't take a linoleum. Yeah, we weren't that cheap I may even have to pack our kids and boxes My wife's pretty happy now You know Matthew's doing okay This is kind of one of these big kind of stressful things. It's where we live now on this in Seattle Seattle's a lot prettier than where we used to live in In in Oakville and because it's kind of flat not interesting. No trees kind of typical subdivision Kind of thing that was out there. This is where I work now on that's the back end of I think that's building 43 there with kind of Mount Rainier kind of poking up over the The hills or there's actually those the the rare time that snow actually showed up in Seattle Which is of course to commemorate my arriving in Seattle. I think this was the second day that we were in In Seattle and this is where I work So just to kind of you know set the stage the the thing that I built before coming to the firm was a bridge right the Ruby seal our bridge and a lot of the things that I'm going to talk about here are essentially related to the problem that the bridge solved which is how do I take a dynamic language and Get it to interoperate with static libraries right because that's sort of the the first step of the problem I because really have to think about it. Why would anybody want to run? Ruby on top of some other guys virtual machine right be it, you know JVM or CLR Whatever it is. It's fundamentally to try and get access to the libraries right that that exists on that platform and this problem is actually a rather interesting and rather difficult problem because I Really don't think that's sort of like just a plain vanilla solutions going to work right So let's say that you were writing. I don't know Python. Let's say right and that already exists If you're writing Python and wanted to talk to static libraries the semantics of how you got to build that interop layer Between the Python dynamic language in the static libraries is going to be fundamentally different right then how you want to do it with Ruby I'm sure there's certain elements that are in common You still got to go off and marshal types around and do conversions at the at the interop boundary or appropriate but you're also going to want to try and do a lot of kind of fit and finish kind of things and The fit and finish things are really trying to make it so that when you are writing a ruby program that talks to CLR libraries and it doesn't feel like you're writing C sharp with a ruby syntax, right? And that's an important element of that as well So, you know some of the things I'll be showing you in this talk are kind of reflective of the thinking that I've done You know in that part of the equation as well and actually what's very interesting as at various points of time I had to compare and contrast and stuff about the decisions of the iron Python team made because Their community is somewhat different culturally right than the Ruby community the Ruby community I find is a lot more kind of Dogmatic right about things like how methods are named right like that's one example of this Whereas the Python community is perfectly cool with that right so one of the things that I'll show here is You know just how you know the differences in that kind of led to different design decisions right in terms of the interop layer between the two different technologies So at this point Like to bring up this slide right this is the the Kathy Sierra thing that I absolutely love right which is This measure of success in your life right your career or whatever it is that you're doing right and you know I'm one of those circles is you know what you want to do The other circle is what you actually do and you know the goal really should be to try and optimize for that thing In the middle right that area in the middle and maximize that area right which is you know For most people those two things really aren't quite aligned right you know especially for the folks earlier on in the Ruby You know of history where you know those two things you had this tiny little sliver Which was between the hours of like 8 p.m. And you know midnight or one or two or however late you stayed up Rather than that other thing up at the top Which is to measure it based on how far you know up some arbitrary ladder that you've climbed and I feel that at least For me, you know coming to Microsoft. It's kind of weird right like I had no idea This is the first big company ever worked at and I managed to successfully never have a full-time job I've only ever had a full-time job for six months my entire life and So coming here is a rather large giant culture shock I'm not just coming to the United States, but you know coming to Microsoft, which might as well be another country and And also having bosses and bosses bosses and all that kind of stuff right which never really existed in my life prior to that But what I find though is even with all of that overhead That that exists in the company that I'm spending like an awful lot of time doing exactly what I want to do Right sure there's overhead like anything else right, but I found that you know the transition wasn't anywhere near as painful as you know I even thought right before going into the company So I want to spend a bit of time now talking about the the kind of more interesting technical pieces of this How many people here right now are writing code on top of? Donate kind of curious Five guys in the room. Okay. How many you guys are writing on top JVM for some other chunk of stuff? Okay, so some other guys So what I wanted to do is just to kind of for most of the people in this room Just kind of show you a really really quick look at Virtual machine environments just so you can kind of understand the kinds of problems and get a better feel for what this stuff looks like and You know the best way that I've ever you know thought of doing that kind of stuff is to just write you Hello, world and show you what happens right and also show you the little different steps and phases that that exist in there So you can see you know what happens when the compiler compiles in a bytecode or iL in our case and what happens after that right? When that stuff goes off and gets executed and I'll also show you I'll show you a static language to start off with and then I'll go off and show you a Dynamic language after that right so you get you know a feel for you know just how different these things you know Actually wind up looking like okay This thing's still working up there good So the best way of doing this is Stay So let's go off and just write hello.cs right as a simple C sharp out Don't you just hate having to type all this? And this is just for hello world right Okay, so that's that's done so I'm surprised at the human compilers in the crowd didn't catch that but There's always that one guy in every crowd right that will you know go off and say but but but that doesn't compile Before you actually get to so we have hello.eggsy there Hello.eggsy does the expected not very interesting thing But what it becomes much more interesting at least on the .net platform is if you use on this utility Called reflector and reflector is a utility that was written by a guy That's just kind of like around the corner for me in building 42. His name is Lutz rotor and Sure Lutz's program is Fascinating right like so to understand a little bit about Lutz. I like I always like telling the loot story which is Lutz is a rather young guy He originally worked on intentional programming with Charles Simone and intentional programming was this crazy research project that Simone ran for about 15 years at Microsoft Which was going to revolutionize and completely change the world of programming and probably the only thing that was really memorable Intentional programming was the fact that they had this super editor Right where you could look at your source code and transform it into different views Right because essentially the editor would edit the AST and they would have these various transformation things that would transform the AST into different things right so they can show you your program as a you know Logic diagram right so you can see it as a logic diagram using different languages You can look at the individual nodes their source control system was integrated into this this on the AST So you'd actually check in and check out individual nodes inside the AST right and that was the granularity at which these things are happened But anyways, you know I think I can't eventually write by Bill and then Simone said fine I'm a billionaire I'll go off and fund this myself and that's essentially what's happening So if you go out and you look for a company called intentional software, that's where all that stuff is So Lutz wrote this program After that because he didn't want to be perceived as being a guy that billed twice at the company right because you know This thing well he looked at it as failure So what's interesting about this program is if you look at any of you minimize this in the task bar right well If I had slightly higher resolution you would see it You'll notice that it's not reflector. It's Lutz Roder's reflector and what Lutz did was He wanted to make sure that everybody at Microsoft knew who he was Right and the only way he could do that was to write an application that everybody at Microsoft would have to use right at some point or another so this is effectively his business card and and his And you so you think well, you know, what's so interesting about this right? This is just you know a bunch of you know stuff right it looks like I'm looking at some namespaces and other things But this is where it gets really interesting. So we kind of dig into this thing and that's the app that I just Type there and if we go dig in a main both fine, right? You can imagine right? We're just kind of reflecting over the metadata right that's describing the type But what's really interesting is if I hit the space bar here and that's what you wind up saying Right, so this isn't ripping stuff out of a pdb file or anything else right? This is walking up to the I L right and if you want to look at it as I L That's what it looks like right if you want to look at this is Delphi because that's what our visual basic or Delphi or whatever floats Your boat right you can go off and look at this so so what is this thing? Well, it's a decompiler right it's a decompiler that essentially goes off and reverses any arbitrary chunk of I L You can walk up to any of the Microsoft system assemblies and and look at all that So you can pretty well look at and think about you know the CLR effectively is being an open-source framework right when you wind Up using reflector right to go off and look at the implementation now Of course tools like this go off and generate an arms race right just like what happens in the Java world right? We're on one side you got the guys that build the decompilers on the other side You got the guys that build the obfuscators and sometimes it's the same company that builds both things and you know So there's there's you know a lot of this stuff, you know people sometimes get really freaked out You know about this thing perhaps not deservedly so but you know nonetheless people are feelings and sometimes they get hurt But this is a really interesting tool for kind of exploring things right So if you ever wanted to spend a little bit of time on a done a platform This is a tool that you should use in your own kind of explorations and spelunking right because this will reveal and show You an awful lot of things especially when you're ratcheted down to this thing right so you can see what the actual I L Looks like so the next thing I want to show you here just so you can get again a little bit of a feel for this stuff is In our in Python app right because this is the only thing I can show you right now Right So if we were to you know do a simple thing like this right and we can go add let's print this What's interesting is what on earth is this thing going to turn into right when we compile it? So let's go off and go and compile this thing and I can never remember the switches for this thing On generate was it no save assemblies So as it goes off it both you know compiles this thing and runs it You'll notice that hello dot eggs. He's a little bit bigger than if I don't remember the number right then then the C sharp thing Now let's go off and look at this thing on a reflector. So hard to navigate this one. It's so big. Okay, all right So now we're into this thing and you'll notice it. Whoa, you know, there's all sorts of scary-looking things in there And if we look at that Let's go off and disassemble this and wow this font is huge. Okay. Let me make this a little bit smaller bump that down to 18 So what you'll see is that there's a number of interesting little pieces in here Like this is the ad method. I'm just so you know, so you see there's an awful lot more stuff here than you might expect right inside of an ad method So we're essentially reflectively invoking this thing here and There it is So this is the actual call so there's a number of helper functions which do things right so there's there's two things I really want to kind of highlight the ad method took x and y as parameters coming into it Right, so you'll notice the first thing is that the type of the parameters is typed as object in the clr for Optimization purposes we Have value types and we have reference types right so things like integers or normally value types These things are passed by value, right? You know even reference types are passed by value right if you're copying the reference right not the not the object But we pass by value of these value types in and we can get all sorts of optimizations right around this thing But of course since this is a dynamic language right and because integers and Python are also objects Right, we don't really have that luxury. We could do some more kind of fancy things But that's not being done right now So effectively what's going on here is that the integers are being passed in as boxed value types Right, so there's this notion of boxing and unboxing On the clr where we essentially take a value type like an integer and we box it And the the act of boxing it is essentially let's allocate an object on the on the GC heap somewhere And we're going to copy right this value type into that object So that's effectively what's happening here. So everything is operated on as as these these boxed value types and of course we have a variety of all the helper functions to do things like add And and other things what's clever about the ad? And fortunately doesn't really kind of show up here because there's a whole bunch of wacky runtime generic stuff That's actually going to happen here is that There are all sorts of different operations where we are going to select and cache the optimal Implementation at the call site as well. So there's a lot of interesting work that's being done here to make sure that The the fastest possible thing is happening right when you go off and invoke it right clearly If I were to show you exactly the same program right in C sharp You'd see there's an ad opcode and bunch other things right which essentially translates it maps directly right to the xcd6 instruction But here there has to be by necessity, you know number of layers in direction And this is where you see you know on some of the artifacts of the fact that we have this dynamic language kind of coming back out here All right So without going into gory hory detail about this kind of stuff I just wanted to give you a bit of a feel for how you know these things change rather Significantly right when you move into a dynamic language and what it takes to actually go off and implement a dynamic language There's a number of interesting other little things here, which people may or may not have thought about and one of them is to have a decent debugging experience right so when you're compiling these things You need to have a mechanism whereby you know what line is currently executing right when you break into the bugger or other things like that So the way this is generally solved as you'll notice here is that there is a dollar sign line a Local variable allocated inside of this stack frame and in here We're essentially going to go off and store in location one right on various numbers right which are well There's only one number there So we only store in location one on the actual physical line in the source code file right where that method was being executed so Those are some of the little details that you kind of have to start putting into your code when you're compiling into this Bycode format so that you get a decent experience when you go off and you want to debug this stuff inside of the debugger or other Things like that. Okay. All right So that's essentially hello world Let's kind of talk a little bit more about specifically Ruby and Ruby interop And right now I'm going to talk about Ruby CLR because unfortunately that's the only thing I can really talk about right now so What we're gonna think about is okay from the real Ruby right the C-based Ruby implementation right obviously that thing's implemented in C You can build C extension functions Or extension libraries right as well. So if let's think about this type called foo We're gonna new it up. We're gonna, you know set some call a data method and invoke a hello method Now this thing here if you were just to go off and build You know a simple C-based extension for Ruby you would have F which refers to the instance You know which has a class member which refers to foo Which is the class object the class object has a method table which at some point in time Points to the C-based implementation right that sits out in my DLL on the other side, right? so that that orange line is the boundary between the Ruby interpreter and and in my extension library Now the method the way you would go off and implement hello on the extension would look something like this right? So this is the var arg style Calling convention that Ruby can go off and invoke the method that would actually handle and do the work I'm inside of hello Now what if I wanted to implement fluency sharp, right? So, you know, what would something like that look like right? So obviously the the on the on the program side of it. It's gonna look identical But this is where things start looking interesting So if we look at this what we have is the On the left-hand side what I what I generate for you inside of Ruby CLR is a shadow class or a proxy class And the shadow class looks and feels and smells and tastes just like the real You know a C sharp class on the other side, right? And so all operations and all method calls always have to be routed through the food class So you'll see that in the method table hello goes off and points to My my C or in this case my C++ CLI code Which contains one of these things called a dynamic method now dynamic methods are a really interesting feature of CLR 2.0 a dynamic method is a little chunk of IL and that little chunk of IL You know I can go off and generate one of these things the runtime will then on the first invocation of that dynamic method Will jit compile that down to whatever native instructions that you want, right? So if you're x86 if you're still running 32-bit Windows And so when it compiles down an x86 code that happens the first time through on all subsequent times It goes off and just calls that that x86 code now the interesting thing about dynamic methods. However, it's a fact that These things if they are not going to be used anymore, right? So if I remove the reference on that left-hand side, right that holds that thing and keeps that alive So there's all sorts of weird little tricks I have to play here right on the left-hand side to hold on to a hard reference right onto that that dynamic method right and there are other tricks that have to play with lifetime management on that side to make sure that you know If that thing went away for some reason right that I would go off and whack that That reference that hard reference of dynamic method it kept it alive now if that reference goes away Then of course a GC can now go off and reclaim the memory that was formerly taken up by the dynamic method So this is a really important thing right because if you want to go off and jit stuff and Run things in some kind of virtual machine environment The problem is is that if you can't either unload the code or some job being almost class So that's fine over there right but in net we can't unload code right before dynamic method So if you created a split an assembly, which is like a DLL if you compiled one of these things it gets loaded in memory It stays in memory forever right so in a dynamic language if you're evalling things or generating code or doing whatever kind of wacky things You do then you have this problem in that if that code if that memory never gets reclaimed right you've effectively got a memory Right so the really nice thing about dynamic methods is it provides us with this mechanism to generate as much code as we want You know securing the fact that as long as we didn't you know botcher implementation that the memory will be reclaimed by the GC were appropriate Now what that dynamic method does is a whole bunch of magic right so the magic inside the dynamic method Does a bunch of things like well hearing let me give you an example method overloaded So in C sharp it's perfectly Legal to allow you to overload functions based on the signature right of the function All right, so both based on the parameter signature as well as a return type signature Even though C sharp doesn't support the return type on part of it, but the CLR certainly does Really however doesn't let you do that at all right So now you have this problem right I want to go off and invoke some method Right and at runtime I must now do the determination So I need to go off inside that method stub inspect the parameters that you're passing in Figure out what types they are coerce them if appropriately To the appropriate dot net types and then find the best match right of the method on the the C sharp side to go Off and invoke right and then on the back side of it I have to marshal out parameters return values all that kind of stuff, you know back to the Ruby side appropriately as well Okay, so those are the kinds of bits of things that have to happen inside that dynamic method on the Ruby side to generate the The the shadow or proxy class I use a combination of constant method missing magic right so with constant missing I use that default in a new proxy class right the first time the constant is referenced and with the method missing stuff That's when I just in time build right those little inter options right so I build them on an as-needed basis All right, so you invoke method like the add method on a rate list That's when I go off and and build this little Dynamic method on your behalf So let's kind of look a little bit at the various bits of features here And you know for the sake of time, I'm going to kind of just step on and go through The slides instead of showing you the code run. I'll show you some code run Just so you know this isn't completely like made up and stuff So this is probably the simplest possible example where I'm going to import the the the array list type into Ruby so so I Do the right things with namespaces and mapping namespaces back onto modules over in Ruby So you can do the include system collections, and now I can just go off an array list I knew this thing Once I've got that I can go off and call the add method the add method go off add 42 into that thing add John and and I also do the right thing back on the Ruby side by overloading the you know the square bracket operator So that you can go off and index into this stuff the way you would expect to be able to do this kind of stuff inside of Ruby as well So that's a simple little example of I'm using that Events are really naturally mapped back on the blocks right so this is an example of not just building the dynamic method Right that allows me to go from Ruby to C sharp right or to the clr This is an example of where I have to build a dynamic method that allows me to go from the clr Back into the Ruby side right so this is what happens when the clr object in this case a button is going to fire an event And I'm going to handle that event inside of a block all right So you can see here that the syntax again is very natural there right I've got you know Okay, button I click set that thing up that's going to map things So there's a lot of magic that happens inside dot click I have to generate one of these dynamic methods bind it to the event on the on the on the button object and Wire it back up to this Ruby block on the other side right so that little chunk of code shows you know simple event handler There as well interfaces are an interesting beast right because We have this thing where in in the in the C sharp and in clr interfaces are first-class Concept right inside of the language now the question is is that well how do I expose this idea right in ruby? Which is kind of a wacky thing so this was my first attempt at getting this and this is wrong by the way And so if you take a look at this there's an array list I'm going to add something to it and my first thought right for what gee how do I want to handle this is Okay, once I've got a I'm going to add a method called the as method right So this is very similar to the C sharp syntax where you can use the as operator right to cast something right to a different type So I can say okay well walk up to a give me the innumerable interface on that and once I've got that Kyle get enumerator once I've got that I need to cast that of course right because I need to tell what type it is back to An I enumerator on interface reference once I've got that thing then I can go off and invoke the methods like move next By the way once you see this kind of stuff You'll also notice my my little name-angling thing happening as well Right so what I what I do is I look for the literal name for the method on the other side right when I create and set Up that method shim for the first time So obviously in C sharp that would be capital M capital and in no underscore right for move next But what I do is if I don't find a name that is that looks like that then I will also try to mangle the name All right, it's a simple algorithm. It's not rocket science here right and And I'll go off and try and invoke it using the other name as well And I will build up that method proxy to call it as appropriate So the nice thing about this is it makes your you know Donate programs feel a lot more like Ruby programs are simply because you don't have this kind of foreign naming convention kind of injected inside of your code Now back to this thing Why is that wrong? Like this doesn't really work this works most of the time right which is like a lot of ideas that you have right you go You pat yourself on the back and go yeah, that looks nice that reads nice is not bizarre or weird But it just can't work the reason why this can't possibly work is that a dot as Right I am numerable the thing I returned from you is a different object, right? Which is pointing to the same real object on the other side That makes sense right because I'll have multiple proxy classes I'll have you know the proxy class for a raid list right the class interface right to the object But I will also have another thing which is an innumerable Proxy right which is a different Ruby object identity right so now I've got two Ruby object identities Referring to the same CLR object identity on the other side. I've broken identity right across this boundary All right, so clearly this can't work, and I need to go off and build something right you'll discover fun things when you shove These things in the hash tables and other fun things like that So we go we got to get rid of that code and we got to make something looks like this This is probably the even though this looks kind of weird because this again looks like Yoda Just walked up to this thing and turned and flipped everything around backwards, right? but I effectively now have to take Ianumerable as a type and kind of treat this thing as sort of like a you know a static method, right? So so get a numerator where I explicitly pass in the instance, right that I want to go off and invoke it on All right, that's the only way that I can go off and do this without Violating identity here right so behind the scenes I can generate the appropriate proxy to make that happen, and everything's fine I can still shove you know e into a Hash and things just work just fine On the other side, um, here's here's some other things that I did here in the array class Right, so this is the array. This is the ruby array class over on this side What I also do is I Also have it so that it implements the Ianumerable interface, right? So the Ianumerable interface now means that oh, sorry. No, this is a dot-net proxy class not the ruby class so on the dot-net proxy class side of the house I have it so this thing also implements Ianumerable and This allows me to go off and build enumerators for this stuff In ruby, so if I wanted to go off and add this kind of stuff This is like the boring stuff here, which is you know the the guts of an implementation of an enumerator The performance of this thing is Remarkable really right because if you factor out ruby loop overhead, right which is very important here If you factor out ruby loop overhead, and you just measure the time spent in the interruptions I get over three million calls a second across that now This is for the trivial case right a marshaling an integer across the boundary and you know getting an integer back There's really not a lot of stuff going on here You know your mileage is going to vary for example if you wanted to take a ruby string and marshal across the boundary Right because if you want to take a ruby string, which is a mutable string right and marshal across the interrupt boundary To dot net strings, which are immutable strings. I have to copy right I have to copy going in and I have to copy coming back Right so of course your mileage is going to vary on those kinds of scenarios in terms of how fast you can get the stuff to go The reason why it's so fast is because I generate these shims and this is actually one of the more kind of really interesting parts of Ruby clr is how those interop of those little dynamic method shims are actually generated these things are all generated by ruby and And so this is one of these kinds of rather extreme examples of you know kind of pushing the DSLE kind of things of ruby to do wacky things Right you know so what I do here is I emit and I generate I L code entirely from Ruby Largely due to the fact that it's just easier for me to do that right the very first version of this this bridge was implemented entirely inside of C Plus Plus and Inside of the dynamic methods. That's really where a lot of change happened and a lot of experimentation and the rest of that and Just the amount of additional syntax goo that I have to type in C plus plus to make this happen Was really painful right so it's a lot easier for me to do stuff like this inside of Ruby I mean the inspiration for this idea actually came from Ward Cunningham when I saw Ward give this talk at oops a little couple of years ago and and Ward gave this really fascinating talk where he was trying to explain to Folks how you can do extreme programming in assembly language? Right because this is one of these questions that He was he was taught teaching an XP class at Intel and you know at the end of the week You know one of the attendees came up to him and said you know wow this stuff is really cool Right you know to you guys as a Java programmer, but I write codecs right in assembly language for a living How does this help me you know this question actually bugged Ward for such a long time that you know It took him a couple years until he actually had to go off And this was all part of this crazy thing that he was showing off at Uppsala where he was building these robots out of these $1 CPUs and he wanted to build this development environment to download stuff onto these robots to get to wave a flag in an aesthetically pleasing fashion right and And out of all of that he essentially built this emulator right for the CPU that he was targeting Right he built that emulator entirely inside of Java right he wrote his code You know trying to force Java to do DSLE kind of things right but the interesting thing was and he could go off and refactor all This stuff right he could unit test this stuff He could do all sorts of really interesting things right for assembly language So a lot of these ideas here were kind of borrowed and stolen directly from that So some of the interesting things I can do is in vet opcodes right so there's the load under this opcode Right and the load under this opcode is really a macro that goes off and expands back into this thing over on this side right so Load under self. Well, you know, that's the scrape the thing out of the Ruby thing make sure I get the self-reference But the stuff inside of the if loads C under I4 and RS that's all I all instructions Right and then I can do some other stuff to conditionally generate different chunks of code based on in this case Whether or not it's a value type or a reference type that I'm actually Retrieving here right so you can do a lot of really interesting things inside of the language including and you know going off and writing assembly language right inside of Ruby Now there's all sorts of Ruby helpers inside of here right so let's kind of take a look at some of this stuff So again when I need to declare an array I need to declare an array of a specific type Right, so if you think about it before we had generics in Donna 2.0 arrays were sort of like the Original generic type right I need to declare an array of type whatever it is in this case an n32 So this is how I inject some type information back into language as well So I can say I want an array of n32 new it up. I want four elements in it stuff those values into that But then coming back on the other side This is the kind of stuff I can do right because I mix in innumerable right so the so the marshaler is smart enough So that essentially what it does is it looks to see the the object that you're marshalling back right? Does it implement the dot net innumerable interface? And if yes, then I mix in the innumerable mix in right at the same time and provide an implementation Right as well So the nice thing is then you can go off and walk over it with collect right and that's actually a dot net array that You're now using from within Ruby right so it's a lot of these old this is not hard right This is I don't know like 10 15 lines of goat or something like that I'm in the thing to make this happen, but you know paying attention to these little details are what really makes this interoperator important Here was something that was actually submitted by Contributor and this is this idea where Blocks can be used for all sorts of things as you all know the only thing that's sort of like a block right and C sharp is this thing called a using statement and the Using statement was added in such a way to guarantee that an object that implements an iDisposable interface is guaranteed To be cleaned up right when it hits the when it exits the scope of the using right? So when we hit the trail and curly in that thing the C sharp compiler wrote some code There's a you know try catch inside of the block. That's being protected right There's a finally section sitting in the bottom right where we essentially guarantee that the object gets disposed And one of the problems in C sharp was the fact that often times you might want to have multiple Disposable objects used in some scope and you'd have to nest the usings right but the nice thing about ruby again Is that we can kind of bend the blocks intact to do arbitrary things? So this is a case here where I can new up to disposable objects class a and b right and inside of n there Right inside of my block. I can go off and automatically call the dispose method on that for you. I Also have support for generics somewhat limited because generics are just pervasive in everywhere So I'll demonstrate the kind of support that I do have and I'll also talk about the kind of support that I don't have So these are the kind of simple scenarios for generics, right? I want a container right so this is a list of type in 32 which is a dynamic list of objects Or it's a dynamic array. It's not dynamic was So you can see the runtime checks in there right so the second ad is obviously going to fail here The the the other stuff about where the generic stuff Is is somewhat limited is the fact that? If you kind of drink the generics Kool-Aid and you start using the stuff everywhere You're going to run into these scenarios where you can have overloads with generic types, you know which the types aren't expanded until runtime right and You know and trying to select the correct overloaded method by inspecting the types of these These expanded or concrete genera types is actually a really really hard problem for you to get correct in all the corner cases So what I don't support Ruby CLR now is allowing you to correctly resolve and find the correct overloaded method Right if you wind up having generic type parameters right inside of those methods. That's a limitation That's it's not something that just can't be done It was just a very painful thing to do and and there were other things that were more important than that on the list So there's this other thing here right which is you know, I can't implement all features So I want to provide people with an escape route right a little Safety valve for them to go off and do some other weird stuff right in cases where I don't have a feature So I don't want to block anybody and so this is one of the interesting Cases I built here so I ripped this idea off of the Ruby inline thing from from Ryan Davis and company and this is you know My my and my analog here in Ruby CLR where I can inline C sharp code or vb.net code or Whatever wacky.net language you want to use Inside of my Ruby program and this was here This was a case because at this time I wasn't supporting Nullable value types of you look at that weird looking bull question mark thing there and dotnet 2.0 We have the ability to have Nullable value types right so this is really useful when you're talking to databases right because you could have a Boolean coming back from a database which could be true false or null right and before there was really no Good way for you to represent the null right coming back from the database All right So these nullable value types turned out to be very useful thing didn't support them So I had to call this API to show dialogue thing which was a implemented using these things So I had to you know put in this little hack and shim to make that happen But you can absolutely do these kinds of things by inlining code So you can inline some C sharp code to solve little thorny problems like this Or you could also inline some C sharp code because hey, I want stuff to run faster, right? And that's a perfectly reasonable thing for people to have as well So that's just the example of using that thing Now this is a thorny case So back to the overloading problem again, right? When I overload I need to essentially figure out what the types are on the Ruby side of the house Right and then try and figure out. Okay, which is the correct method to call the other side But if you look at those two method signatures, that's perfectly legal right in C sharp, right? Because it's in Charles are two different types, but guess what right? There's no inch our type inside of Ruby So there's absolutely no way that I can tell those two methods apart right by inferring right? I can't resolve the correct Method here, so what I do there is I come up with a mechanism where I Build these kinds of shims right so the shim here is I'm allowing you to manually say hey Right in the case where it was the n32 char case The name of the shim that that I want you to build will have that name right that that name in yellow there Right, so I'm allowing you as a programmer to disambiguate on these cases that I can't figure out for you on my runtime Right, so that's a useful feature to have in there as well because you know Even though this is going to be a weird corner case right when you run into one of these things Then you'd have to do a weird C sharp thing or something else and that's just not going to feel very good Right, but these kinds of problems are definitely exist no matter what dynamic language you're using To go off and talk to I'm static with type libraries that might have this kind of stuff inside of it This also has a nice kind of side effect in the sense that because I'm binding exactly to one method one of the design points inside of Ruby CLR is that if I know that a method doesn't have any overloads Right, I have this fast call path where I don't try to run my overload resolution algorithm, right against You know the the the types only do the type conversion I call it straight in right so this has a side effect of actually being faster right on the interop boundary right because I don't Have to attempt to do overload resolution inside of the shim that I generate for you there so There's this technology right from Microsoft and I like making fun of these guys because it's just it's almost too easy, right? So like a lot of companies there's this kind of fixation on XML right this kind of weird kind of a natural fixation and And XML is great for a lot of things But it's certainly not great for people to edit right if human beings need to edit this stuff Now in the defense of the guys who created this technology right which is the XAML stuff right XAML is a new presentation Well XAML isn't got anything. It's just a markup technology right XAML is a way to describe a tree of objects Right just think about it that way right and to you know set properties and do a bunch of other things with this That's really what XAML is now. There are different technologies a layer on top of XAML like windows presentation foundation All right, so what's happening here is this is an example of WPF expressed a XAML right? Where some tool is going to spit this thing up right so XAML itself as a file format was fundamentally designed for tooling Right because in trivial little cases like this. I'm a menu a bunch of other things Yeah, sure you guys could do this stuff right I can do this kind of stuff right But if you ever see some of these crazy wacky things that you can do in flash for example Right with animations and gradient fills and nice vector line drawings and meshes on 3d surfaces You're not going to take that stuff in by hand right nobody is right You know so you're going to use some kind of designer tool for that stuff anyways And that was really the primary scenario for this But I still feel that there are going to be cases where I'm going to want to go off and build small applications right in XAML Right like this. So what can I do to make that a little bit better? Well, this is a case where I could have that other file Right as an external file. I can load it in and do a bunch of little things here right to make it happen I'm going to load Wpf.load will go off. That's a little helper method I wrote which will take that editor out XAML return me the object right that the XAML Parcer instantiated on my behalf I can call fine name on that find something click it print something You know you get the idea, but what this thing does? I can make it suck a little bit less by doing this right so I could take fine name and turn it back into the Square Bracket Operator right one of the nice thing about this is that in a static Remember remember about these proxy classes right that I create I can effectively write something that looks like this I Can now shove this thing into framework element Which is the exactly the right place right to put this thing in there and all other Ruby applications that call into that API Right from the Ruby side of the house are now going to be able to use this right which is a nice thing to have Right this is something you're used to but you're not used to doing this kind of stuff calling a static library right? And that's the part that's really interesting. I think that there's a very interesting space For folks who really like designing API's right to go off and take some sucky API right on You know the static library side and build a really nice usable wrapper right To go off and use that stuff on the other side All right, so I think there's a lot of little design things that can be done like this To make things better. We can even take it a little bit more Further than that so that's essentially taking the original XAML thing using some of the wackier Ruby metaprogramming stuff here Right to go off and allow me to define The elementary or the object tree that I previously defined inside of XML But entirely inside of Ruby and the interesting thing about this is and I don't have an event handler in this case But the event handler gets scoped Appropriately based on Ruby lexical scoping rules, which is actually really important because XAML has wacky lexical scoping rules Right, which are not what you would expect right, you know So this stuff actually makes things significantly easier if you are building these kinds of relatively simple applications Right where you're not doing all the crazy 3d stuff and the gradient fills in the rest So let me show you the really kind of fun thing I want to show you here is this thing that I hacked up On a dare So if you're gonna go off and build Software on dynamic languages right one of the real valuable things at least for me is her Right this idea that I write a line of code. I execute a line of code or I know the line of code and X you know line of code and You know what if we can let's you know We have all this hardware on these boxes right which are a lot better than you know console things Right, you know, so why can't we try and use the technology right to provide a better interface right to for doing Consulate types of things and as you can imagine I'm a big fan of black And I really like you know this kind of minimalist thing and so we can go off and do things like you know write some Kind of a better herb so this was the first stab at writing a better herb that I did a while ago So I can go off and run this thing. I can colorize this thing inside there I can invoke this thing over here. I can you know get some IntelliSense showing up there, you know Right that can go off and run This this this is all of this Windows presentation foundation stuff up here that I'm that this thing is built using So I can even go off and put in you know some really nice formatting right as well so this is something that I stole from the the the Ruby cookbook right and So I just type this thing in and format it a nice way so you can get an idea of what you know it'd be really kind of cool if I can now take help right from some other system or from a book like this and Integrate this directly into your Erby experience, so I don't have to alt-tab into some this is almost sort of like the Emax School of Thought right coming back in here right Where I can bring the thing into my one true shell right so I can now look at it inside of there and The nice thing about this is unfortunately because I had to do this wacky resizing thing. This thing is size for a much different Windows size I Can't show you the really what cool stuff But I could like you know zoom in and out on this dog this document and do a bunch of other things right which are Really pretty interesting as well. I have some other thing here where I can just kind of show some documentation and this documentation I actually Extracted all of the documentation for all of the types in the dot-net framework are available via web service On MSDN so this was a case where I just kind of manually massaged and did some transformation against this But you can imagine writing some code to do this where you can say give me the docs for file Give me the docs for a rail list and this stuff would just be sucked in over a web surface and dropped into your little interactive console Thing in here as well right so this thing is already using Ruby CLR and Ruby This is a bit of 300 and some odd lines of Ruby code to make this thing happen Right, you know, but this is kind of at least approved to show that you know You can actually do some things right using this interop layer in the shims and the other things that we have in here cool so Now the so The problem really is is that You know if any of you guys were here at RubyConf before You'll realize that you know This is more or less the same talk right and I apologize right for that fact in the last six months that you know In my defense essentially I was involved in a really in seenly complicated move that sucked up every waking moment that I had And but once that's done now, that's over right there's stuff happening now, but unfortunately I can't say anything but you know the good news about the stuff is I am actually working on stuff that you would think I would be working on and and If you keep your eyes open on April 30th Right, there's a conference in Microsoft runs called the mix conference and at the mix conference You will see what I've been working on since I joined the company and a whole bunch of other really interesting things right the whole other interesting things by the way right when When I accepted the offer, you know to go to Microsoft It wasn't until all that other stuff that you really have no idea about right now, right? That's actually the much more interesting piece right around this and dynamic languages I think play a very important role in that other really interesting thing right, so April 30th don't bother going to Vegas, right? It's don't need to do that right, but just you know keep an eye out on the internet and you'll find the announcements coming out of there If you really are interested in this stuff the conference to go to is PDC which happens in October Right because like a lot of things or you tend to announce things before you have like, you know The great bits ready right by PDC. I'm reasonably confident We will have pretty great bits ready right for things for you to actually play with right So if you actually wanted to play this up, that's a really good time to go You know invest the time if you have the time to do that Okay, I think that's it guys have any questions about this thing You know, I'd be happy to answer any questions that I can't so the question is You only have a power PC system. You don't have parallels. You can't run parallels Therefore you can't really run windows in a way that doesn't really horribly suck right because virtual PC is just a horrible way of Going right because it's really really slow and So what can you do could you run this stuff on top of mono? So there's fundamentally nothing about anything that we're building right that would preclude running on mono right like As far as I know, there's really no APIs that we use that they haven't you know implemented on the mono side But on the flip side is we're really not actively doing anything there But this is one of these great things for the community to pick up right if so this is exactly what happened with the iron python project Right so on the iron python project. There's a source forage project called fee pie right iron python FEP y right and so the fee pi project Essentially tracks the iron python releases on windows and and essentially does whatever wacky things They need to do in order to guarantee that it works on top of mono, right? So so definitely there is this one data point, right? You know, that's already happened in the iron python community, right? so question is can you call Ruby CLR using mono not today and And I actually spent some time with Miguel trying to figure out why and he blames it on the C++ compiler, right? So the thing about mono right is they don't have a C++ compiler, right? so they only have a C sharp compiler and and Apparently right this is words out of Miguel right is the fact that the the Microsoft C++ compiler Injects a whole bunch of wacky attributes into the managed assembly that is runtime.dll and the thing that I built And those attributes aren't supported by mono But what Miguel thinks is that all it would really take is for some guy to write, you know pearl script to walk over So what you could do is disassemble it into I L right using I L DASM, right and and run a Pearl script over the same to rip out all the attributes that that the mono doesn't support and then Feed it back through I L asm, right and you should be able to make that work, right? Or a Ruby script for Some regular expression eat thing, right? Yes Yeah, it's in it's up on it's up on Rubyforge now, right? It's been up there for a while Yeah, there's a whole bunch of wacky dependencies on it, which are kind of semi-document it, right? But you know, but I got it to work on a pretty, you know vanilla installation here, so okay cool. Thanks again