 We have a fine fellow all the way from the great state of New York. He's one of the organizers of one of the many Ruby meetups out there. He, like a lot of us, fell in love with Ruby through the innumerable class. He's gonna be talking about that. I've heard a lot of people say, if I couldn't use Ruby, then I had to use language X. As long as I could take a read with me, I'd be alright. I feel the same way. So, next, we have Habis Amin, and he's gonna be talking about how he fell in love with Ruby. My name is Harry, so I kind of have a deal with him in life, so you know, this is how it is. This is my first program. This is very fond of this area. When it comes to Ruby, Seattle R&D, and thank you guys for the guys. It's not in a mirror. I'm definitely looking forward to talking to more of you guys. So, who am I? My name is Harry Habis. I work for a company called Daily Burn. I guess I'm a software web developer. I live in New York City. I live in Brooklyn, technically. They have just a part. I want to show you a part. It's close to the city, alright. But yeah, I'm very active in the Ruby community in New York. I constantly go through the meetups. Co-organized over the New York group meetups. It's just like a small gathering. We usually cap it to 20, 25 people, and it's like a round table discussion. We custom each other to provide opinions, and you know, we hack on stuff. So, it gets really intimate. So, I've had the pleasure of living in New York and being part of that for over a couple of years now. And I'm loving it, and kind of one thing that I get asked about people who approach me first or try to get to Ruby is like, hey, what's your part about Ruby? And that's the topic on my top. I'm kind of an evangelist for all things that I really like. So, just mongromatter. I had to say that because all my friends bet me that I would not get away talking, not mentioning mongromatter, but it's a little better. But anyways, I digress. Yeah, so I try to, you know, the purpose of this talk is for any of you guys who are kind of new to Ruby can I introduce you to some cool stuff for the new role for module. And there were things in there that I knew more before, but I'm a little bit more familiar with now. So, there might be something more for a more experienced Rubyist like it happened. But it's really more maturely. So, let's do that. I work for Daily Burn. We're kind of a health and fitness company. We have some iPhone apps in that domain and calorie tracking and work that stuff. It's a really, really fun place. But really, what's most fun is the shenanigans that we do in the office. That's me just, you know, rolling on a ball. That's good for your back. So, you guys should do that as programmers. Program on the next schedule is really good for you. And that is our CEO who is doing shoulder presses with one of our colleagues. This guy, we've done so much stuff with him. We've rolled him around. We've done bench presses with him. It's not me. But yeah, we do all sorts of crazy shenanigans like that. It's a really, really fun place to be. So, I'm at the stage in my life where I'm actually very, very happy and satisfied with my career and where it's going. I love the community I'm in. I love the tools that I'm using. I'm excited to go to work every day. It's, you know, knock on the wood. I'm really happy to be where I am. And I think most of us in this room would probably be in that situation. Because if you weren't, you probably wouldn't have taken time out of your work day or weekend to come to a talk or conference like this. So, where did I come from? I studied physics in college. Took a little programming, you know, wasn't by any means a major or minor in CS. For me, programming always used to be this thing that gets stuck, you know. I had to crank out some integral, you know, do some, you know, differential equations or something. That's like, you know, I just had to get stuff done. I didn't quite enjoy the programming classes, the acting classes. The most one I had to work on was in my research. And that was, you know, where I was actually using it. I was like, oh, this actually makes sense. I'm doing stuff with it. But I don't really care about what's going on. I just want to get the result that I'm looking for, you know. So I was in that mindset. And I never thought that I'd be working. I was like, I can't be sitting in front of a computer every day for the rest of my life. I guess everyone's doing that now. Not a program. So that's kind of how my attitude towards programming was, even though I've been doing it for, even before college, a little bit. Not super seriously. So the last one I actually gave was a research program. This is Galactic Fluids. You can read this. Half of this doesn't mean anything to me anymore. That's sad to say that I spent, you know, two years of my life doing something, and now it's like I'm immersed in something completely. Well, not completely. I guess I'm completely different. I'm starting to forget the stuff I did and my degree, but I'm really happy where I am right now. To tell you the fact that it was just, I'm going to program and get stuff done. And that's it. It was going to be physics all the way from here. So one thing that I learned was that, you know, physics formulas don't actually pay bills. So I had to find a job after I graduated from college, and I was really like, physics was a shirt I don't want to do, but I grabbed Studies Level, and I was like, all right, I'm going to get it. I guess I'm going to get a programming job a little bit more longer. And again, Accountant Blessings, where I got into a really awesome small startup in Indianapolis, where I was doing Ruby work and Rails work, and a lot of suspended stuff too. So I really, really, really started enjoying what I did. Like, I really, really, like, look forward to everyday work. And the first thing, when I got into that drove me nuts, is how ridiculously awesome the racing hatches are. They're actually fun. I mean, I can't tell you how annoyed and frustrated I was iterating over stuff. For loops and sees job. I just hated it, like, to the bottom of the cross. Like, there has to be a more, there has to be a better way to do this. Now, obviously, you know, we're, by no means am I, I'm still the opposite of a polyglot. A monobot? I guess so. But, you know, nothing like this. So, you know, I indulge in other languages too, but just, really, for me, just got this down really, really well. And I was like, wow, my racing hatches are really, really fun. They have all these cool methods, and they just, I love the syntax and the readability of these methods. Like, I can't look at them at a method, and not even look at an example, be like, okay, I probably know what this is gonna do. So, that's actually very powerful in my opinion. So, what took me a little while to figure out, was like, hey, all that stuff is powered by this innumerable logic. So, it raised hatches, and in 1-8 strings, I guess we're neural now, and we're either quasi-neural, they're not technically mixed in. Files do it, sets and ranges also use the innumerable. So, what is a neural module? It's a mix and it's a module. Like, all modules in Ruby, it's mixed in to get some add functionality to whatever class you're mixing it in. Essentially, it gives you a bunch of methods that allow you to work with collections. The most basic constant collection, of course, is an array, and that's kind of the basis of a lot of stuff. So, a lot of good methods, in turn, would return arrays, unless you're dealing with hatches or sets and ranges, and then it would return those, respectively. But yeah, most notably, it powers the array and hatch classes, and that's kind of what I'm going to be talking about mostly today. So, how do you mix innumerable in your classes? You have to have each method. So, as soon as you define the each method, you essentially have innumerable objects. Your methods collect, select, and all of those, they essentially take the yielded output from each, and they perform their respective behavior. So, here's a couple of examples. We have a class called Planet Express. No, here I'm a big control of M. You can go up totally out of there in this evening, but I will catch up at this time. So, yeah, we just included a neural module there, defined in each method, and it's very simple. It's just yielding four strings, Bender, Fri, Leela, and Toipa. Nothing fancy here, and then that collect is able to work because it is aware that there is each method defined available to this class that will allow me to do what I want to do. So, most of the methods in the neural module will work in that way. Why is it so sexy? I mean, okay, so I'm sure you can mix it in, right? To me, it's this. I think these are all the methods. I might have missed a few. They're available in the neural module, and it sounds like crazy nonsense, but I actually really like them because I look at all of those, and right off the bat, if you're kind of familiar programming and come from another language, you're like, oh, you can probably figure out 70 or 80 percent of them, but I have no idea what they do just by their names. So what I was trying to find was this talk, and I couldn't find it, and I think it was from someone respectful from the Java community who said it's kind of ridiculous how many methods are available in our list. Essentially it was a critique. It's like, oh, there shouldn't be so many aliases, there shouldn't be so many methods available in a list class, but I actually really do like them because it allows us to think of it in paradigms. Even if it's the same method, the reuse method in 1.9 is actually alias to inject, and you have concepts like, oh, math reuse isn't just this notion of, you know, I do, I've been living in Mongol worlds. This is basic stuff that we can do and Ruby empowers us to implement data structures and concepts that are familiar in other languages, even if they're not available in core Ruby, it allows us to do that. And the unimum module is an excellent example. So, again, for me it was programming happiness. Everyone's seen this code here in other Ruby conferences. I think at least every Ruby conference it's standard, at least one person must show this code, and I'm glad that I'm the one through here. Programmers often feel joy when they can concentrate on the creative side of programming, so Ruby's designed to make programmers happy. The last part, Ruby's designed to make programmers happy. That's exactly what the unimum module does for me. It's exactly what dealing with arrays and sets and file does for me. I hate it dealing with files and seeing. I'm sure most of you would agree. But yeah, so that's that's great. So now we're going to just quickly gloss over a few innumerable methods. Some of these will be really basic and most of you are probably familiar with them. There'll be a little tidbits here and there throughout the top. We've got three or four Drs. So the doctor is in. So let's look at some methods. Each basically allows us to iterate right off the bat. I feel I guess the the Englishness of each makes perfect sense to me. I'm exactly what's going to happen. I can give it a block and there are two do's there which is a typo. Ignore that for now. But yeah, essentially it yields items that are supplied in the block and you can basically implement it in a pass when you're including an innumerable and you get a lot of other stuff for free such as fine. Just a nice little method that basically finds one record given the condition of the block. It's kind of cool. It's the simplicity of it. As we start going through some of the stuff try to kind of think of like the other stuff that we do on a daily basis such as active record which most of us are from Israel think about how it's doing its own stuff on top of an innumerable. So you think about associations in active record so you get an array back so you have as many a blog has many posts but you can't just you can't just you can't just add stuff or you can't use the add operation on that. It actually gives you not a lot of operation error because they've actually you can't append to it at the record. It has its own very specific way of dealing with how an array should behave. It has its own concept its own criteria for what it is. It's really cool because it allows you to make your own data structures that are based on other data structures. A really, really great example of that is like the Mongo Ruby driver it's probably one of the best libraries in Ruby that I've ever come across I'm not a big fan of it but it's a good it's a good exercise in going through taking just very limited structures and looking at arrays and hashes and all things that are known to us and that we're very comfortable with as Ruby is and see how you can implement it to deal with a story in a nice way. This is kind of an interesting one. I've actually never really used this but I can totally see how awesome it is. So you have basically it takes a blog and it returns a hash and the key of that hash is actually the return value of the work. So let's see so right now we're taking all the names probably under Lila and Zordberg and where the blog of what it's doing is just looking for all the names that are longer than that that's actually wrong that should be the count. So that should be named count right? So then the count would be 4, surprise or let us long so that's why the key is 4 so that's a little bit of that but do you guys follow me here in the room? From the same page? It's been a long flight alright? New York is far but yeah this is cool think about how you can use this as a little mechanism right? Just doing a tag cloud if you have all these frames or all these values just want a quick tag cloud in they're already indexed by the keys already you know the value of the cloud which is kind of interesting right so you can just be like hey I want 10, I want something that appears 10 times or more than 10 times I think you can iterate over the keys and just get back without having to iterate over the whole hash which is kind of cool. Gramp is awesome I think we're all somewhat familiar with the unix environment that's kind of how I actually got my first project in the program but you actually have plenty of system and stuff so we can kind of do that too so hey cool this is cool and I love how Ruby kind of stays true to some of those concepts and uses them, uses the same vernacular so it's like not only is it easier for someone from another program language to have that environment to come around but also if they're not familiar with it if they go, like I've seen this in meetups where some guys are starting to learn Ruby, they're not necessarily from the unix background and they're like hey oh I have on the command line I can actually do grab that's how Ruby does grab and that's amazing but not only is Ruby promoting it's the easiness of it to learn it but also it's promoting like hey go check out this other supporting borrow ideas so it's actually just this pattern one kind of important thing to note that it uses the triple equals operator for pattern matching which brings us to our next doctor's regret tip which is cool so you can actually not only grab for a pattern, like a regex pattern but you can actually grab for objects so if you have lists you can say like hey I want to grab for strings or an instance of something which is it's powerful, I've used it a few times and some of the stuff I've done it's definitely one of the things that I think gets lost over and mapping collection, I actually had the hardest time with mapping collection, I was a little afraid of them when I first got into Ruby, I kept playing using EGE even though I kind of knew that they're probably what I want to use but I think the problem was I don't like the name mapping collection, I think we should think of it more as a transform so that's kind of how I think of it so hey, here's a collection and I'm doing some transform on transform on that collection and the result is an array that's executed that transform, that makes more sense to me in my mind but others might disagree so map is obviously very popular, we're going to look at some cool things we can do in map later on, it's definitely different than each return essentially it's collared, a map returns the transformation that was done on the collection now we're going to talk about the enumerator a little bit essentially if you take a look at let's see, so we've talked about how we can create basically give renewable methods, make them available to pass by mixing in that cost but there are also other ways to get those methods without actually doing the whole mixing process, without actually including the module and the three of them that I can work across, there might be some others actually too, from some monkey patching and math program but these are fairly straightforward in my opinion and common practices you can now create an enumerator explicitly with a code block, attach an enumerator to another object, or you can create an enumerator implicitly without a block so this is the first method so basically what we're doing here is we're calling the enumerator class which again includes that enumerable and we just when you pass the block the why there is actually the yielder, it's actually the instance of the enumerator yielder, so you never want to actually yield and you never call it yield when you're using this method but it's a pen to the yielder so just quick little tip there the other way is you actually attach the enumerator to another object, this is actually a fairly popular a fairly popular scheme, I've seen it in a bunch of libraries basically it knows the enumerator knows how to implement each based on the method that you're passing it so here we're saying like hey I want you to implement each using the select method and because select is essentially doing essentially doing what I want each to do so you get an enumerator out of that too but I'm going to enumerate to the select method for this name to write this enumerator a nice application of that and the last one is we can create an enumerator implicitly without blocks by just calling it enumerators and this is really really cool stuff I actually did not know this until I started preparing for this block so here basically I have a map and if I don't pass map of block I get an enumerator so that's kind of cool but why do we care? because if you use this and there are a bunch more but I just did a label a couple of them perhaps you want to add an enumerability to an existing object so an object that maybe you don't want to monkey patch and you know including enumerability you just want to for your main instance you just want to add the enumerator like any select map so enum4 which is the second way that we talked about basically method in that object in that class that acts like each but isn't each but you can give it all the other enumerable methods by tongue it's like hey since this is really what I want these to be like consider that to be consider that for my enum for my enumerator so I'm going to say survey parts is actually is actually going to map to what I want these to behave like obviously this isn't necessarily how you want to necessarily write something you probably just want to regularly include enumerable the class but it comes in handy if it's a library that you don't want to necessarily monkey patch or a certain instance of it and it's out of your scope for your main fine-graded generation is kind of cool I kind of saw some people talking about this in my research it's basically an enumerator is not great but it has a notion of state so it's not just an enumerator enumerators have no sense of state so you can essentially you can say something like hey give me the next thing give me the next thing and we'll rewind which would be kind of cool and I haven't this is a toy an idea but it would be interesting to see how you can implement a state machine in such a way which would be definitely an interesting exercise so the coolest thing is like you can actually change some text which for the most part doesn't make sense we'll talk about that so normally if you change map and then you change select with it you might as well just do names that's solid you're just essentially you're passing on map is basically just passing on the values of your ray to solid it's just piping it through it doesn't know what function it applies so there's nothing going on there so most enumerators that's always have it so changing enumerators for most part doesn't really make sense doesn't do anything except in a couple of ways in a couple of places it actually comes in handy this is another story for tip lazy slicing so the e-slicing method which is awesome I use it a lot especially when I make some view helper stuff you know if you're doing carousel views and stuff like say 15 or 20 you know books and you're having jared fur plug-ins and stuff doing that but anyway so lazy slicing you can tell it each slice you can pipe that to map and basically what this gives you is when you normally want to eat slice say I have 15 items it would create 15 or if I had 20 items and I'm slicing it by 5 it's going to create slices 5 so that slices with 5 items in it each right and so load that into memory and that's going to be memory however if you chain it with map it gets lazily evaluated so map will only use it when it needs which is definitely like a nifty little tool not sure if you're if it's a huge optimization boost that's a different point but it's definitely if you can have little things here and there then why not right you would have to create through the power of enumerators and map with index so we have each with index which we all love I love it first time I came up I was like oh awesome I don't have to keep track of the index like another local variable that I'm going to throw away later on this is great so map with index and there have been some ruby threads on it people like hey we want this in core ruby it should be here you know it's just like each with index and it hasn't implemented yet I don't know if it ever will but we can actually implement it ourselves which is pretty awesome I found out about this like a urine tube ruby and I use it all the time you can chain with index which is an enumerator method with map and basically you have the same power and that's awesome because it's something that's the with index is a generic method it's not tied down so you can you can chain it with other stuff too so that's the last tidbit or doctor's word for your tidbit so let's talk very briefly about the set class and explore the set class in 1.9 which uses the enumerable module and just as a it's a nice exercise to both put stuff up on ruby and see what's going on at least any of it that you can understand a lot of it might be in office KFC code but if there's stuff written ruby especially stuff in fact that you should totally go check it out so set class is standard in the class on ruby you have to explicitly require it and the store is basically unique unordered values and it does use the enumerable module in this you can see here this is the code straight up from ruby code from the standard list so it includes the enumerable module it defines each and it's very very clear what's going on in the class it calls up a block for each member of the set and it's also passing the member as a parameter and if there's no block in it just returns the enum for underneath flair's set is actually using hashes which is kind of cool I found out personally a lot later than I think I should have hashes and ruby the keys are always unique which is kind of a nippy little tool to use we were doing this ruby games before we just hacked on little problems and one of them was parsing a log file and you know unique entries in a log file just as an exercise and we were trying to we had benchmarks and we were competing and I learned this through that exercise with some of my friends like hey, once you the keys on a hash is always unique so you can just change the key it'll always guarantee that you're never updated you're never going to have a non-unique key so what the hash is doing is basically it's storing the values as keys what set is doing is storing values as keys in a hash and here's just a quick example of how it's set as its own notion of what includes should be just how we were talking about actual factors and many relationships even though it's an array it has its own notion of what adding stuff to an array should be and it's essentially checking it's essentially executing the include method on hash so it's actually using intervals to the level it's using the hash class underneath that's how it's defining its own implementation there are also other methods in the set class that I encourage you guys to go check out and look at how they're doing things their own way to preserve what a notion of a set is and using basically just overwriting innumerable elements to match their own use case so are you in love with Ruby yet if you weren't it's very disappointed and you don't have to be a fanagomist there's a lot of good stuff out there not in this book, this was in inspiration for a lot of this talk David White is really really good at this this is one of those books you want on your bookshelf really it's good to grab and look at the year down the road this was in 2009 it's really one 9th edition it's still relevant right now actually the book before this that you wrote, Ruby for Rails is actually another really really great read because it shows about the techniques in Ruby like all the the dynamics and the how Rails uses it to achieve what it does and this was kind of a read write of that and it has an excellent section on innumerable so yeah that's going to get out and I just have this one last thing to say that really really whenever I talk to someone and I'm like if there's something that you don't understand come to me and I'll help you figure it out I kind of want you guys to pass that along there's got to be something in Ruby that you really really like because I bet you you're going to fall in love with it all over again there's how Rethics was sort of dealt with ranges metaprogramming all the things in Ruby that I've been doing for a while and you kind of forget you know there's it's always good to go back and see why you like language and why you fell in love with it and realize there's no reason to fall in love with it all over again and the best part about it is they'll always help you transmit and appreciate the next thing you learn or the previous thing you learn so with that I would just say thank you and go make some love now with the what you call the other things on it and I just want you to expand it with the iteration just because we are actually going to use it that way and what are the possible exceptions that there will be I'm actually not sure so the question was the example of using essentially the iterator example to my gradient ratio where I was using next I've actually used it as a toy app for like the scenes thing that you saw basically kind of like a state machine for scenes I'm not sure what any performance implications it has but it's definitely I'm not sure if I'm going to actually use it for an actual state machine it's just kind of an idea but I what about the nature of actually having an exception in your experience in exceptions what you usually acknowledge about the exception you should talk to an update who's here I've written a really great PDF book on it which I guess I read in purchase but I'm not aware of that thank you guys