Garden City Ruby 2014

Analizando las tres alternativas disponibles para desarrollar extensiones nativas para Ruby

Tejas Dinkar  · 




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

yeah so hi everyone and like Swan and said I'm also part of the team that's organized this so I want to just say it's really awesome to see like this huge crowd on a friday morning in Bangor so what I'm speaking about today is native extensions

in rubies I love my talk is native extensions serve three ways about myself I'm dejaste just incur I'm a partner at Newlands Oh software which employee-owned collective and it's really fun place to work if you want no more catch me tomorrow on

Twitter I'm tea drinker and on github by him gja you can find most of my open source contributions over there so about my talk this is actually a pretty technical talk but and so expect to see lots of code I hope to have five minutes for questions I'll

try to beat like cetacean beg for it but like if it's if I don't have time just catch me in the hallway or whatever and and we I can try to answer whatever I can so I'm mostly recovering see extensions ffi and swig and yeah so let's talk about

first why you would even want to do why were you would ever want to build a native extension for Ruby so there's there's a bunch of different reasons so number one is to maybe integrate with new libraries say like a new database has come out like for

example lib drizzle which was a new library that came out to work with my sequel in the drizzle database you might want to put that over to Ruby you might want to improve performance of critical code there's different ways of doing this of course you could

like try doing JRuby look at different caches but sometimes you have an algorithm that you just want to implement in native code or there's already a great library that implements it someone is giving me an example of csat I think which saw which bundler

could have actually used to or could use to kind of resolve gem dependencies so that's written in C++ and sometimes you want to just move that there and to improve performance and of course you want to write code that works across different languages and

in general it's a lot of a lot of fun you know like real hackers program in C and all that stuff so you can just feel super elite by doing this so before we talk about native extensions I'm just going to take a small Segway and ask so let's talk

about Python a little bit like how many people in the house are like Python Easter's how many how many big Python fans well that's actually surprisingly small I thought there would be more but so I've actually figured out the best way to write

Python code and i'm gonna i'm going to tell you this right now and yes of course I am trolling you so over here I have a Python interpreter open and i'm going to say import Ruby yeah that seems innocent enough and i'm going to say Ruby rhod

evil fubar'd sighs hmm that seems to return six so that seems to work let's try something more complicated i'm going to say Ruby eval I'm going to define a method factorial and yeah def factorial and partial N equals zero partial factorial

n minus 1 etc yes its tail recursive i knows and finally i'm going to call it and i'm going to say Ruby devel factorial of 5 yet again you'll see that I actually get the result right so wow what is this have i implemented a Python in Ruby let's

let's look at the code which actually makes this happen how can I actually make this work well surprisingly it's about a dozen lines of code I know this might be a bit hard to read from the back so I'm just going to I'm going to read out the

interesting bits line by line so it's about a dozen lines of code but let's remove all the Python stuff and let's just look at the Ruby parts of this it's even a lot less yeah so let's go through this one by one I start off by by including

Ruby dot H Ruby dot H from most of you who are familiar with C and C++ dot H files are header files they basically have the definitions of the various constructs that Ruby or like whatever that your library exposes so that you can so that your compiler knows

what what definitions exist in the first place so any Ruby extension that you write you'll need to include Ruby dot H and most of the time this is completely sufficient you don't need to include anything else so let's let's start looking at

the actual code I have a method called Python Ruby eval yeah and it accepts one parameter which is a which is a string okay nothing complicated there what I do next is I take that string and I pass it to a function called RB evil string right which stands

for Ruby evil string nothing really magical over there all I'm doing is I'm calling Ruby's evil and you'll notice that the the RB evil returns a object of the type value this value is actually very important the same way in Ruby every single

object inherits from object the corresponding construct in the see Ruby extensions is the value object value object is used to represent every single Ruby object from nil to true to false to every single custom object fine so i have this value object what

[ ... ]

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