Coding 101 65 (Transcript)

(Net casts you love from people you trust. This is Twit! Bandwidth for Coding 101 is brought to you by Cachefly at

Father Robert Ballecer: This episode of Coding 101 is brought to you by Casper on online retailer of premium mattresses for a fraction of the price because everyone deserves a great night’s sleep. Get $50 off any mattress purchase by visiting and enter promo code c101.

On this episode of Coding 101 we’re breaking into a brand new module. That’s right folks, it’s time for Ruby. Hello and welcome to Coding 101. It’s the Twit show where we let you into the wonderful world of the Code Monkey/Code Warrior. I’m Father Robert Ballecer.

Lou Maresca: And I’m Lou Maresca.

Fr. Robert: That’s right he used to be our super special guest co-host but now he’s my co-host. He is the official co-host of Coding 101, Lou and you’re in studio. This is kind of cool!

Lou: Yes, super excited to be here.

Fr. Robert: And actually you’re going to come back each and every single week right?

Lou: I wish I had the dough to do that.

Fr. Robert: Yes I know and unless we develop teleport technology. Now Lou we’ve got a special treat. We’ve been promising people that we were going to get into some of the more web based languages and we definitely have 1 for this module. We’re going to give people a 4 episode module for Ruby and Ruby on Rails which is the framework in which Ruby can run. I got to tell you we did pre-record these interviews a while ago but it is fascinating what you get with Ruby. It’s a different way of programming.

Lou: Yes it’s actually a really good language and it’s one of those virtual based machine languages that can really get you going fast and up and running as soon as you can.

Fr. Robert: Yes exactly. That’s one of the things that I learned early on with Carlos Souza who’s going to be our Code Warrior which is the whole idea of the language is that it’s built for the programmer rather than the machine. Most languages they feel machine like, this is really trying to help the programmer.

Lou: That’s right and I think the 1 thing Ruby really is fantastic for is really getting yourself going from just installing, really quickly downloading something and installing and running it right away. So that’s 1 of the things it is really positive for.

Fr. Robert: But before we get into that you know we do have to cover the news and you’ve got something interesting. What’s this all about – virtual machines for rescuing web performance?

Lou: Yes, 1 of the things that Facebook has had problems for in the past is a lot of their components and their Facebook website is built on PHP and PHP is not necessarily known for the most performant of platforms that they have. So what they came up with is they devised a new system and it is called HHVM which is called the Hip Hop Virtual Machine. It’s basically a way for them to enhance performance when they do things like server side – basically allow them to code in a language that will then compile down to some kind of version of that language that then at run time the machine can just interpret it a lot faster and actually be a lot more performant.

Fr. Robert: Right. Now this is in the vein of languages that try to do just in time so it’s not interpreted, it’s not quite compiled. You only compile it when you need it. Which is supposed to give you a nice hybrid mix of performance/usability/functionality. So in your experience what separates a good just in time language from 1 that’s just not good at all?

Lou: I think it’s the virtual machine. It’s really the 1 that actually can take the IL languages, intermediary language that it is jetted down to and then what the virtual machine can do with that. So a lot of them can be platform unspecific, platform agnostic, in this case this 1 is and like for instance C Sharp it is starting to get there. A lot of these are like that but the ones that really can handle different platforms and be able to scale are really the ones that make the jit the best.

Fr. Robert: It’s interesting that this is for PHP because PHP is 1 of these languages that it was very early on in the transition to a web based programming world and there’s a lot of people who want to kill it. They say PHP should disappear entirely but it stubborn because it is installed in so many different code bases and it is very useful and I think it’s gotten better. I mean we can say that right? It’s gotten better over the years but 1 thing that even those who support the language will say is it is not a performance buster. PHP has never been something that you use to optimize. In fact you want to stay away from PHP to optimize. So with this announcement is Facebook saying that they think they can keep PHP as long as they use this just in time technology?

Lou: This gives them the ability to write code, that’s the whole idea – write code once and be able to reuse it and make it do things that they don’t expect it to do. So that’s really what they are doing here. They do write other different parts of Facebook in other languages but again here it just gives them the ability to reuse what they already have and then slowly move off of it later on.

Fr. Robert: Alright. Now let’s get down to brass tacks because people are going to say ok that’s nice, I’ve heard this 100 times, every week there is… it almost feels like every week there is another new pet flavor of something, either a framework or a language that’s got a little bit of sugar to make it better than the previous version. Let’s talk about performance. Because, and I’ll use your experience here – because you live in the dot net world, how does this compare to dot net? And I think Zach actually you’ve got a chart for this right?

Lou: There’s a bench mark that we’ve seen in the past is that PHP dot net is made, it is built on top of the dot net frame work, we have the CLR which is the run time and the code behind files are built in C sharp or BB or whatever and you compile those down. What you can actually see from this chart is you’ll see HSP dot net raw being right around the same as the Hip Hop Virtual Machine, so you’ll see a massive amount of performance gain that you’re getting. It is kind of hard to see in there since there is a lot of framework in there but if you do a quick search of ASP on the page you’ll actually see the – there’s HSP and then right above it if you just do a quick search of HHVM you’ll actually see that it is literally a couple lines above it. So that’s kind of the key is it is slightly faster in some cases. At the top of the screen they’ll let you switch to different types of like for instance doing queries or data sets and you can see the performance comparably. It will sometimes even out-perform HSP dot net. So it is actually showing quite a bit of promise in this whole jitted land of compilation.

Fr. Robert: Ok bottom line it for us, who should be using this?

Lou: So if you’re already on PHP or using Open Source, using Apache and that kind of thing and you’re already on PHP and you have a big code base like Facebook or a little bit smaller than Facebook obviously then this is a good alternative for you because you get the raw performance out of it that you have before. But if you’re starting from new it all depends on if you want to stick with Open Source, if you want to move over to the Microsoft world or that kind of thing and you get the raw performance there automatically. So it is really depending on the eco system that you want to be in when you first start out. But if you’re already on PHP this might be a great alternative for you.

Fr. Robert: I think that’s mostly the way that I feel which is yes there are better languages than PHP for many things but if that’s what you’re programming in and that’s what your code base is in then this is a viable solution rather than you chucking everything and you starting from scratch. When we come back we’re going to go ahead and start speaking with Carlos Souza from Code School. He’s going to be our guru taking us through the shadowy realm of Ruby and Ruby on Rails. But before we do that I want to talk to you about something that’s near and dear to my heart. Sleep…no seriously sleep. Lou, I laugh, and people laugh about this but we all know the statistics, we’ve all heard the stories which is you spend maybe a quarter or up to a 3rd of your life sleeping – closer to half for me. I do like to sleep, I don’t get much but I do like it. The funny thing is that even though we know what happens when you don’t get enough sleep…you’ve seen it, you’re a supervisor; when people don’t get enough sleep they make bad decisions, you end up with really bad performance. It is those zombie hours where you’re sitting there and you’re just staring at your screen and you really can’t think of anything to do. That’s all from sleep deprivation, we’ve seen it, we all know it but even though we know that you need your sleep it is amazing that most people still aren’t willing to invest in a good mattress - something that will help them get more sleep out of the time they invest in it.

Lou: That’s true.

Fr. Robert: You haven’t run into that right? You get a good night’s sleep every night.

Lou: Right.

Fr. Robert: Oh wait you have kids so…

Lou: I haven’t slept for 5 years straight.

Fr. Robert: That’s why we are very happy to welcome to Coding 101 Casper. Now what is Casper? Casper is a place for you to go online to get a high quality mattress. Now there’s a few things that you need to know about them. 1st they cut out the middle man. They don’t want a showroom. They don’t want a sales person. They want to sell direct to you because what it means is that they can offer you a better product at a lower price. Now their mattress is obsessively engineered and it’s at a fair price. It includes 2 different technologies, latex and memory foam. Anyone who’s had a memory foam topper knows that it’s nice but you can sink into it and it gets kind of hot. And if you had a latex mattress you know that it isn’t so comfortable. Combine the 2 and what you get is this perfect blend of comfort and support. Leo LaPorte likes it and you know you will too. Wow! That actually did perform quite well. I’ve never actually seen that piece of video. I like it. Casper will give you a comfortable mattress that has just the right sink and bounce. Leo got his Casper mattress and he’s never looked back. You can buy it online completely risk free. 1 of the things that I like is that they give you a 100 day return period so you can actually try it in your room. Now imagine what that means…most of us if we actually put the money into buying a decent mattress, we’re going to go to a showroom where you’ve got 90 seconds, maybe 2 minutes, maybe if the sales person is real generous you can lay down for 5 minutes and say ok well I want this or I don’t want this. That’s no test at all, you need to know how you’re actually going to sleep in the thing, when you toss, when you turn, when you get up in the middle of the night. That’s why Casper will deliver this to your house where you can try it for over 3 months before deciding whether or not it’s right for you. Friends you’ve got to try Casper. These mattresses are made in the United States and I got to tell you they’re high quality. Now if you’re ready to make the upgrade to good sleep go ahead and get a Casper mattress. It is $500 for a twin, it is $950 for a king size which is comparable to industry averages which means you’re going to get an outstanding price because again they’re cutting out all those middle men. And you can save an additional $50 as 1 of our audience members by going to and entering the promo code c101. That’s and the promo code c101. A better sleep awaits, why aren’t you sleeping on a Casper, and we thank Casper for their support of Coding 101. I’m sleepy now.

Lou: Let’s go.

Fr. Robert: Let’s go, we’re done, alright thanks. No but seriously now it’s time for us to toss over there to the sky desk where we’re going to have a nice little chat with our Code Warrior Carlos Souza. Welcome to the 1st of our Ruby programming modules. I’m Father Robert Ballecer tossing from Father Robert Ballecer…yes it’s one of those time space continuance things and we have joining us a programmer, a developer who was actually 1 of our wild card episodes not too long ago – Mr. Carlos Souza who is a developer and a teacher for Code School. Carlos thank you very much for joining us.

Carlos Souza: Thank you very much for having me here. It’s a pleasure to be here again.

Fr. Robert: Carlos we brought you here because you are an expert in Ruby and we’ve been wanting to take a look at Ruby and Rails for quite a while. You’re going to bring us into it and give us that first taste to see if maybe this is the programming language that they need. Let me start off by asking, and we covered this a little bit in the wild card episode; why would people want to do Ruby. What do you think is the draw for the language?

Carlos: Ruby is 1 of the very few languages if not the only language that was built thinking of the programmers writing the code rather than the machines that would run the programs. So it’s a very expressive language. I want to say it’s an intuitive language and extremely powerful, extremely rich and again it’s a pleasure to work with.

Fr. Robert: Well let’s jump right in and give people an actual coding example. Give me a concept that you can program on screen to show people why it was designed for the programmers rather than the machine.

Carlos: I already have Ruby installed in my machine. If you want to install Ruby it’s not super hard. You can go to the website and follow the instructions. More specifically if you are in a Windows platform you might want to check out the Ruby installer. This is probably the easiest way to install Ruby on Windows but I’m not going to go through the installation process. I’m just going to assume that you have something running. The 1st cool thing about Ruby, let me just go ahead and close this. So you can see I’m here on the terminal, also known as the command line. This is where we run all of our lower level commands in the computer so if I want to list my files, LS, you can see that I have 1 file here. But before we dive into this file I want to show you something called interactive Ruby – the Ruby console which we’ll fire up using the command IRB. So IRB gives Ruby prompts so it can actually write Ruby code that’s immediately evaluated and outputs to the terminal so if we say something 1+2 is output the result of that, of this very simple mathematic calculation.

Fr. Robert: Very nice, the last time we saw something like this was when we went through our Pearl module. It’s sort of a real time environment for us to evaluate lines of code as we go.

Carlos: Yes exactly. So Pearl has that, Python is another very popular language that has that same interactive console and Ruby is no different and it’s probably the easiest way to try out Ruby on your local environment. I use this a lot to just experiment with different things before I actually write them in my program. This is 1 of the coolest things that you can be aware of when you’re starting out with Ruby.

Fr. Robert: Carlos let me clear something else up really quickly because again you mentioned this during the interview. There are IDEs for Ruby and for Rails. You can download very nice graphical frameworks that will check your work as you go. But you’re 1 of these programmers that you prefer not to have any of the training wheels. You just compile your code and you let it go right?

Carlos: Right, yes I just use a very simple text editor. Since you mentioned compilation there is no such thing, that step is not something we developers have to do in Ruby because it’s a dynamic language. It’s not a compiled language so we don’t have to run anything compiler and then generate the binary and run that code. We just run a Ruby script and then the Ruby command parses the source code and then just outputs the result.

Fr. Robert: Right, that’s the strength of the language. Alright keep going. I’m sorry to interrupt your demonstration here but let us see the magic.

Carlos: Sure. So you can output commands using “puts” and “puts” is the same thing as print or echo in other languages. So hello everybody, and it’s going to return the result here at the bottom as you can see. So this is kind of like the easiest way to get up to speed. Just kind of try out the language and because we’re going to be writing a little bit more code I’m just going to jump into a file and write something in that file. So let me just go ahead and clear this out, script and then we’re going to start with a blank slate. So like you mentioned I use VIM which is a text editor that’s been around for a long time. It’s not, I don’t want to say it’s super easy to use but it’s very light weight and doesn’t offer any of the benefits of the IDE so maybe because of that reason it tends to be faster because again it’s just a text editor. But we do have IDEs for Ruby and for Rails out there, multiple alternatives… I’m just going to create a file called Script.rb and then inside of that file I’m going to start going the same command that we ran. So hello world, the typical 1st program that everybody writes. So I’m going to save that file and then run the script with the Ruby command. So if you run that you can see the result is printed on the screen. So that is sort of our first introduction to Ruby.

Fr. Robert: Very nice. That’s the 1st command that they know, that’s the 1 that will allow you to push to screen.

Carlos: Right, exactly. So let’s now work with variable assignments. So let’s say that we have a message that says; Hello my name is… and what we want to do here is add a touch of dynamic to this code. So we don’t want to hard code the name in here. Let’s use a variable to replace that name. So we’re going to do something called a string interpolation. So in the middle of that string we’re going to pull the value from another variable and replace it with this. So I’m just going to call it name for now.

Fr. Robert: Nice.

Carlos: Then above here we can say name: Carlos.

Fr. Robert: And of course the people who’ve been watching Coding 101 for a while will notice that you’re not assigning types to your variables because that’s not how Ruby works. You’ve got dynamic type assignment.

Carlos: Exactly. Very cool that you brought this up! I am definitely not determining the type because like you said it is dynamic typing and Ruby does something that’s called type inference. So when you assign a value to a variable it determines the type of that variable and in run time it gives that value its type. So if we now put message and run this code Hello my name is Carlos. And I can go back and change this to something else, run this again and it’ll replace with that name. So this is a feature that’s called string interpolation so again we’re writing a string and then in the middle of that string we have a place holder that calls out to a variable that is assigned 1 step above here.

Fr. Robert: I’m really starting to see where your statement comes “this was designed for programmers”. This is a much easier way to consider how you would catenae strings and how you would use variables inside of a language that doesn’t really consider the computer, it considers the programmer. I love this, this is nice.

Carlos: Right and we’re just getting started. Let’s go ahead and start a function or a method. So the way that you do that in Ruby, you declare methods with the key work “def”. So def and then you give it a method name. So let’s just call this print message. Print message is going to receive 1 argument which is going to be the name. The name of a person, so we’re going to move this inside of here and we can kill this. So define a method that takes 1 argument name and uses that argument to build a string, assigns that string to the variable message. 1 thing that we don’t have to do in Ruby and we have to do in a lot of other languages is to turn a value. But the thing is Ruby assumes that the last expression in your method will be the return value for that method. So we don’t have to do this and we also don’t have to do this. This is all we have to do inside of our method so that it returns the message with the name. So instead of message I’m going to put print message and I’m going to say Carlos. So I’m going to put this and then you can see it ran our function with our argument. Go back and call something else…

Fr. Robert: That’s just a value that you’re passing to the method.

Carlos: Exactly. So now because we’ve wrapped our little string interpolation instead of a method we can simply call that method from where ever we want the method to be printed. So we can sort of like reuse that same logic from different places.

Fr. Robert: So this data inference, the type inference in this case is inferring what the return is… This is a theme through Ruby right? I mean it seems intelligent enough where it just kind of figures out what it is you’re trying to pass or what it is you’re trying to return.

Carlos: Exactly and you notice I’m not on the method definition, I’m just simply saying there’s going to be 1 argument. I’m not saying what type of argument that is going to be.

Fr. Robert: Something is inbound and I want you to take that something that is inbound and put it with this string.

Carlos: Yes exactly.

Fr. Robert: Wow.

Carlos: Exactly that is what's happening.

Fr. Robert: Very nice. Okay, I see that this is nice, so this conference theme in Ruby is definitely something that's going to be good for a programmer. But we're going to have to go through some of the standard pieces that we need for any programming language like symbols, like loops, like blocks, like classes. Do those change much from what you might see in say, C Sharp?

Carlos: The concept doesn't change much, but the syntax does so in Ruby, we do have objects, we do have classes, in C Sharp, we do have objects. We do have classes but the way that we write down and to some extent, the way that they behave are a little difference in Ruby than in other languages.

Fr. Robert: Let’s take a look.

Carlos: Yes, let's take a look. So if we wanted for example to give a little bit more information about the person, perhaps we want to say the location that person is from, so let's go ahead and instead of passing the name were going to pass a person and here you're going to see person name and I’m from person location. Right?

Fr. Robert: Got it.

Carlos: So the message would be hello, my name is the name and I'm from a given location. So how do we do that? Let's build a person class. That's how you build a class in Ruby and were going to use a constructor to set the name and the location for that person and in other languages. I'm not exactly sure if in C sharp but I know in Java the constructor is named after the class, but in Ruby all the constructors are called initialize, initialize, and the constructor is the method that gets called when you instantiate an object in that class. This is object orientation. This works across all languages, it's not any different in Ruby and were going to initialize the name and a location now I'm going to use these two arguments and store them inside of an instance variable and the way that instance variables look in Ruby is variables prepended with an @. So in this case name and the location. So because we assigned them to instance variables we can now read from those instance variables but we still need… If we try reading them now - I'm going to go ahead and intentionally force this error. So let's create two people here, let's create Carlos…

Fr. Robert: We’ve got the Strengths in the chat room right now who is asking a very basic question. He's wondering if case-sensitive matters in Ruby. Yes?

Carlos: Yes, it does. Ruby is a case-sensitive language. You're in San Francisco is that where you are?

Fr. Robert: Yes, I am.

Carlos: Alright, so now we're going to send Carlos and Robert as arguments. Now we try to run this code now it says undefined method name for person, so let's go back and see what happens. So we try calling name here from inside from our object but this name method does not exist. So we have to create a name method in our person class. The name method is nothing but a getter. And what a getter is, is, a function or a method that simply returns a value that is stored inside of that object. So, one way to do that would be to create a method called name and return name and then the same thing with location.

Fr. Robert: Nice.

Carlos: So this is one way to do it. Now we run this code you can see, my name is Carlos I'm from Rio. My name is Robert. I'm from San Francisco. But in Ruby there is a shorter way to do that we can write something called reader and say name and location. Run this code again and it will behave the same.

Fr. Robert: How did that work? How were you able to shorten all of that creating those two methods within this class down to just using the reader?

Carlos: Right. Again because Ruby is a language that thinks about the programmer that is writing and they try to give you all the tools they need to write good and clean code - this is called encapsulation because we want to be able to hide as much as possible and only exposed to the developers what they need to know because this is a common pattern in object orientation to expose getters they give you… they as in the Ruby language; gives you a shortcut to create getters, which is this class method called attribute reader that takes 2 arguments, 2 symbols and this is a symbol in Ruby which is basically a string that always points to the same reference in the machine; the same memory address in the machine. It is sort of a lower level optimization. Because when you create two strings in Ruby or in pretty much any other language - so name and name…so line 5 and line 7, although they look the same they are 2 different objects and they point to 2 different memory addresses in the machine. But if we use a symbol these 2 are exactly the same and they point to the same memory address in the system. Ruby utilizes that to reduce the memory footprint that it needs to run. So whenever possible we use symbols instead of strings and this is 1 of those scenarios. So we call the attribute reader, and this is nothing but a method right, because we can omit certain things in Ruby… so yes it would be the same thing if we called this with … So run this it’ll be the same or if we omit this we can also omit this. We don’t need these or these, or these. Actually we might need the last ones, let’s see.

Fr. Robert: We’ve got a question from a member of the audience who is more used to C sharp and he’s wondering how this is handling the instantiation. I see where the class is defined, I see where the symbols inside those classes are defined but where do I actually instantiate the class here?

Carlos: Right here when you call dot new.

Fr. Robert: Nice.

Carlos: You call dot new on a class and that creates an object that we’re assigning to Carlos here and then to Robert here on line 16.

Fr. Robert: That was a huge chunk we went over in the C sharp module. Thank you very much.

Carlos: So you see we didn’t need to compile anything, it simply ran the Ruby script and parsed it all and it generated this code. Now let’s go ahead and extract this print message to the person object itself. So let’s say greet and this greet is going to be this message here and instead of calling person “dot name” we can actually reference the instance variable from this person. So we don’t need the print message any more. And instead of saying print message we’re going to say greet. Does that make sense?

Fr. Robert: Yes.

Carlos: And we actually don’t need the attribute reader anymore.

Fr. Robert: You keep adding code and taking it away and our audience is going wait a minute. What do I actually have to write?

Carlos: Am I going to fast or?

Fr. Robert: No, it’s perfect. They are kind of amazed. They’re saying this is actually how most people would actually think about doing this but they are accustomed to having to work around what the IDE does.

Carlos: Right. Again like the last step that it just did the attribute reader, I can pretty much just leave this here and it would still work.

Fr. Robert: It’s just you don’t need it. You can have it there.

Carlos: Exactly, we don’t need this so what we want to do is we want to write code that’s easy for someone else to read and understand. So if someone else reads this code as it is and notices there’s this line here declaring 2 getters they will assume that these getters are being used somewhere right? But if they’re not then you’re misleading whoever is writing this code so we’re much better off not using it at all.

Fr. Robert: This is probably the language that requires the least amount of commenting. I would still comment everything but it’s so clean, it’s so concise, you can just follow it and go ok I see what’s happening here and oh there’s where we have the puts.

Carlos: Another nice thing you can do too – we can do this assignment in the same line.

Fr. Robert: Ok. Now there’s a question; how does white space work inside of Ruby?

Carlos: Most times it doesn’t really matter. So I can put as many spaces as I want here and it’s not going to matter. It’s not white space sensitive like Python is, not at all. Now this is personal preference, this is valid Ruby code but for method definitions my personal preference is to always use perretts, just for consistency.

Fr. Robert: You don’t have to.

Carlos: We don’t have to, no and again it is just my personal preference but it’s interesting that you mentioned consistency because I think when it comes to participating in a project with others – consistency should be more important than personal preference. So what that means is if you join an existing project that follows a certain pattern you should follow that pattern even if it’s not of your liking.

Fr. Robert: Right, I was just going to ask you about that. If you’re working on a team with Ruby because there are so many different ways you can do the same thing it sounds like you would have to have at least opportune team meetings to say “well this is how we’re going to do it, this is how we’re breaking out methods, this is how we’re doing classes, these are what we’ll expect to see in your code so that we do have that consistency across the project. Do you find that more important in Ruby than other languages that might be a bit stricter with how you program?

Carlos: Absolutely. I think it’s a very important part of working on a team, having everybody agreeing and you can do that either through in person meetings where everybody discusses and talks about those conventions or you could have a document that specifies the code conventions for your company or for your project.

Fr. Robert: Right.

Carlos: I’m not sure about Ruby but I know there are other languages, Java for example; I know it is part of the build system to run a…sort of a code audit that would check for those specific things. Because again in Java for example although it’s a stricter language it can still do things in different ways. What it would do during built time is to make sure that you’re doing things in the way that the project… following the projects convention. So there might be something out there for Ruby that does this automatically as well, which is another way to enforce project conventions.

Fr. Robert: Carlos, we are starting to run close to the edge of time for this particular episode but I’d really like to see how Ruby handles loops.

Carlos: Yes, loops, absolutely. Let’s do that. So here we have 2 people right, Carlos and Robert. Instead of repeating puts for these 2 people, what I’m going to do is create an array. 1st I’m going to create an array called people and I’m going to assign Carlos and Robert. This is how you create an array in Ruby. I’m not creating an array object manually, I’m literally just using the brackets here, using 2 elements assigned to these people and Ruby knows this is an array. I’m going to say that people each do person and this is a loop in Ruby.

Fr. Robert: That’s a loop?

Carlos: Yes, it’s a loop and it’s usually called an internal iterator and then you can say person dot greet and then if we do this now it is going to print our messages for us. This is the Ruby way to do it.

Fr. Robert: Wait a minute. How’s that working, how do I get the parameters for my loop?

Carlos: Exactly, this is the parameter for each person. So what Ruby is doing is again this is an array right, so if you do people class, you can see it’s an array.

Fr. Robert: Of course.

Carlos: And the array has a method called each which is what we’re calling here and this method takes 1 argument. The argument to the each method is a block, this is a block in Ruby.

Fr. Robert: Nice.

Carlos: A block is kind of a…for those who are familiar with Java it is almost as if we’re passing an anonymous function, which might make more sense for the Java script developers. So we’re passing a block and this block takes 1 argument and inside of that block we’re calling the greet method on the argument that’s passed to the block. So this is what’s happening behind the scenes. Now another way to write this which might make more sense is let’s call this person block and assign the… that I was talking about. So person puts person dot greet right. So now instead of passing a block like that I’m going to pass the block as the argument. So think of this as an anonymous function in Java Script. If I pass that, that still works.

Fr. Robert: Wow.

Carlos: Again this is what’s called an internal iterator. We’ve giving, the collection, in this case the array an iterator something that needs to be done for each element and we’re letting it up to the array to know how to loop through those elements.

Fr. Robert: So basically when you handed it to block it’s smart enough – again this is a theme in Ruby – to know that you’re going to want to run through the entire array.

Carlos: Yes, it’s going to run whatever is inside of the landau for each element of the array.

Fr. Robert: We’ve got web6054 in the chatroom who’s getting a little lost on lambda. Why do they call it lambda, what exactly does lambda define?

Carlos: Lambda is almost like an anonymous function pretty much. So the code in here doesn’t get executed right away. If we simply had pus person dot greet, something like that or even a put oh hi… if we did this over here this method would be executed as soon as line 19 was interpreted. But when we wrap something inside of a lambda what we’re saying is don’t run this code just yet.

Fr. Robert: Right, get the whole thing first.

Carlos: Yes, get this code, store it in this variable and send it to someone else. That someone else will know when they need to run this code. When that someone else determines that this code needs to be run they’re going to run this code. So basically they’re going to call the dot call function. Let me give you another example here. If we simply have this and run this code it’s not going to run. We didn’t put anything. But if we did…I think its call, if we did this then our code is run right.

Fr. Robert: Nice. We’ve got people right now whose eyes are glazing over. To those people I say don’t worry because remember this episode will be available on the download, all of this code, all of these examples will be available in the Git Hub that we’re setting up for this particular module. So you can move through it at your own pace as many times as you need to understand what’s going on.

Carlos: Yes absolutely and before we move on I’d just like to show the more typical way of iterating through an object which is using the external iterator. Again the Java Script developers in the room might be familiar with so we’ll do “for person in people…

Fr. Robert: That’s something that our audience knows what it looks like.

Carlos: Yes. So puts person – p and puts are the same thing. I think it is for person. So let me go ahead and just comment this. So for person and people do… let’s try to. Yes you see that?

Fr. Robert: Yes.

Carlos: So that’s the more typical way of doing it but it’s not what we call idiomatic Ruby. It’s not the type of Ruby that’s written by “Rubiest”.

Fr. Robert: This is from a “Ruby-tourist” basically.

Carlos: Yes, for someone new to Ruby or just visiting…

Fr. Robert: We’ll make sure that snippet ends in there because the people we have coming from different languages are probably more comfortable with that. They can try that a few times because it will work and then they can move to something that is more native Ruby.

Carlos: Exactly. 1 last thing I want to show if we have time is what’s called open classes. Let me go ahead and just comment everything in here. Alright, my thing is not working. Nope.

Fr. Robert: Oh, that’s kind of like a comment.

Carlos: Nope, one last try.

Fr. Robert: It was working just a few seconds ago.

Carlos: Alright, we’ll get back to it. Let me just delete it for now and we’ll leave that snippet in there. So you see that we declared our person class up here and then we created objects from that class. Now what I want to do is add another method to this class. Ruby is so flexible that it allows us to re-open existing classes and add things to those classes. So 1 example is if I reopen the person class I can just declare another person class but since we already created 1 it’s going to realize that class and… say goodbye. Right, we can have the say goodbye method and say I am going back to location.

Fr. Robert: So this is just assuming that you’re going to take everything that was in the previous class and use it now in addition to this.

Carlos: Yes.

Fr. Robert: Why would I do this? Why wouldn’t I just go back up to the original class?

Carlos: I’m going to show you why in a minute. So if we now do Carlos say goodbye and Robert say goodbye... right so we’re calling the methods that were defined when we reopened the class and if we run this script, oops, did I forget to say something? Yes I forgot to put puts. Alright so I’m going back to Rio, I’m going back to San Francisco now. You might ask why you would want to do that right. You have access to the person class. I created a person class, why not just stick it in here? Here’s the thing, you can also do this with the classes that are built in Ruby.

Fr. Robert: Oh! Ok now that makes sense.

Carlos: Right. So let’s go ahead and just do something with… let’s do this. Let’s have, let’s give a year, for example the year I was born, 1984 and say print age. So this is something that I just want to create right now. If you call print age we don’t have this in Ruby.

Fr. Robert: Right.

Carlos: So age puts age. If we run this code it’s going to blow up and say there’s undefined method called print age for this which is the type fix num. So the 1st thing we do here is hmmm – fix num is the class so let me go ahead and reopen the fix num class, define a print age method and say… let’s do this minus self. And then self is the actual value here so 1984. So if we go back and run this it’s going to give us my age. So notice here we reopened an existing Ruby class, defined a custom method and call that custom method on 1 of Ruby’s primitives. This is what allows for example Rails to do stuff like 1 day ago. This will print out literally the date of yesterday or 10 weeks ago. This is valid Rails code. You can do stuff like that and I can actually show you where Rails does that. If we go to Rails…

Fr. Robert: Actually we’ve got a good question from both Roscoe and from Dale in the chatroom and they’re wondering about replacing an existing method inside of a class.

Carlos: Right, that is bad. That’s very bad.

Fr. Robert: Is it still possible?

Carlos: Rails gives you that flexibility and you can still shoot yourself in the foot. That’s why it’s not good to modify existing methods and there are also arguments that say it’s not good to add to existing classes but this is basically how Rails was built on right. But the only reason that they do that, that Rails does that is when it makes sense and when it makes your code more intuitive and more expressive. This is called monkey patching by the way. It’s the name of this practice of reopening open classes and adding functionality in there. If we do core extensions here and go to time – no let’s do a numeric, numeric time. I’m going to make my code a little bit bigger and easier to read here. So yes you can see 1 example here. What Rails did was literally opened the class numeric which is a built in class from Ruby’s standard library and it added its own methods. So it declared the method seconds and then that’s implementation and it added a bunch of other methods. So in Rails, not in Ruby but in Rails you can call 2 dot minutes and it’ll give you how many seconds that is. Many other utility methods that again make your code easier to understand. So just try to think for a minute how much code (if you’re not writing Ruby or working with Rails) in other languages you’d have to write to determine the date from say 2 weeks ago. You would likely have to do some math right or the code for 2 weeks from now. You’d probably have to times how many in a day, times how many days in a week, times how many weeks in a month kind of thing. In Rails you can simply do 1 dot month dot from now.

Fr. Robert: Very nice. Carlos I’m afraid we are at the end of our time for this particular episode but we’re coming back next week and we’re actually going beyond Ruby. What will we be doing?

Carlos: We’re going to be writing our first Rails application, hopefully using some of the stuff that we used here. Kind of exploring the different parts of Rails and what Rails gives us out of the box to make us an extremely productive web developers.

Fr. Robert: We’re going to be going back to the studio in just a bit but before we do that don’t forget that all of this code, everything that Carlos just did will be available in the show notes along with homework. Carlos, what homework are you going to give to the folks at home?

Carlos: Homework for the folks at home… with everything that we learned here I want to see if, especially with this monkey patching thing, I want to see if people can implement the following code. So basically what’s on my screen here, can you see this? So I want people to implement this. To human number, and what this needs to print out is one. And then 2 – to human number, needs to print out two. So I want to see if people can implement this from 1 to 10. That’s it, simple stuff.

Fr. Robert: Simple stuff but it’s going to take a while. Folks don’t forget that you can also find Carlos at Code School and Carlos can you please tell the folks at home what Code School is, why you’re at Code School and where they can find all of your work.

Carlos: Absolutely, Code School is an online platform for learning web technologies. All of our courses, the majority of our courses are in the browser. This means that you don’t have to install anything in your computer to try out a different language. So you can see here I’m on the Ruby path and we have all of these courses just on Ruby. These here are about Rails, how to build Rails applications, how to build APIs in Rails, how to test in Rails, how to use a testing framework and we have a bunch of other technologies as well. We have stuff on Java Script, HTML, CSS and iOS so if you’re a Windows machine running Internet Explorer you can still see how it feels. So this is Code School. You can sign up for free, start taking some of our courses, about 30% of our content is free but if you want access to everything you pay $29 a month and that gets you access to everything single thing that we have. All of our courses and also all of our screen casts. So we have screen casts that follow up to our existing courses and we also have other video series that dive into different things. We cover existing apps, we try to replicate some of those App’s functionalities. We go on and we meet with people in the industry, we have a video series with David Heinemeier Hansson which is the creator of Ruby on Rails and we talk a little bit about their approach using Rails for base camp which is the business that originated Rails; and a bunch of other cool stuff, Groupons thread list and that’s basically what Code School is in a nutshell.

Fr. Robert: Fantastic. Again Carlos Souza from Code School thank you so very much for the knowledge. We’ll have you back next week. We’re going to be diving into some Rails but right now it’s back to you Lou and Padre at the Coding Desk.                               Lou, what I liked about that is Carlos really brought out a lot of the program friendly features of Ruby, this idea that it has internal iterators, the idea that it does smart dynamic typing which we’ve seen before but it just figures out what kind of data you’re trying to push and it will automatically set the type. I’ve seen some of that in C Sharp but I haven’t played with many web ready, web centric languages.

Lou: I think that’s 1 of the advantages of Ruby is that it can actually get you going and it’ll handle why types and type specific things that you’re doing and it’ll be able to determine at run time and even at compilation what you’re doing.

Fr. Robert: And again this kind of feeds into what we talked about at the beginning of the show – the ability to compile things, to make things in time as they come up is really where I think we’re heading and Ruby is a very nice example of that.

Lou: Exactly.

Fr. Robert: Now next week we’re going to be bring Mr. Souza back, he’s going to be showing us not just Ruby but Rails. That’s right, you’ve probably heard about this, they go hand in hand. You can program in just Ruby but that’s kind of like running a race with 3 wheels. You should have the full set which means it’s time for Ruby on Rails. Now don’t forget, we know that this is a lot of information but we’re going to give you all the assets. Mr. Souza has given us his notes so we can make sure that you get them so you don’t have to copy off the screen. All you’ve got to do is go to our show page at or coding 101, it all goes to the same place and you’ll be able to download those notes either from our Git Hub or from a Zip file depending on how we make this work. We’re still trying to figure out why things don’t work some times. It’s weird like that. Don’t forget that you can reach us online, socially. I think the best place is socially. I don’t mean for a drink or anything but the best place to find us is on Twitter. Lou, where are you found?

Lou: @LouMM and feel free to see me anytime.

Fr. Robert: Right and it’s not just a show you work on, you actually do work for Microsoft.

Lou: That’s right.

Fr. Robert: You are 1 of their lead developers, you run their teams, you’re a manager and your main project is CRM right?

Lou: That’s right, you’ll find all my work at

Fr. Robert: So if you want his help with CRM and actually Jeff Needles here in the studio really wanted his help and he said no…make sure you find him on Twitter. You can find me at that’s @padresj. If you follow me you’ll find out who we’re going to be speaking to each week, the topics that we’ll be covering and you’ll actually be able to suggest guests for future episodes. In fact Carlos Souza was suggested by you on Twitter. So if you’ve got a favorite programmer that you want to either see helping us program or see in 1 of our wild card episodes make sure you tell us on Twitter. You can also find us on G-Plus. You’re in the group right?

Lou: Yes I love to be in there actually.

Fr. Robert: It’s filled with smart people and the nice thing about that group is you’ve got beginning programmers and you’ve got expert programmers and everyone in between and people just help each other. Trust me if you have a question, even if you think it’s stupid ask it in that group. Just go to google plus, look for Coding 101, it’s a great place to get the support of your fellow programmer.

Lou: Exactly.

Fr. Robert: Also don’t forget that we do this show live, or most of the time live on Mondays 2:30 PM Pacific Time. Just go to If you check out the live feed you can see what we do before the show and after the show and everything that goes in between and you can also jump into our chat room at Lou, I think that’s about it. Should we wrap this one up?

Lou: Let’s wrap it up.

Fr. Robert: Until next time, I’m Father Robert Ballecer.

Lou: And I’m Lou Maresca.

Fr. Robert: And this has been Coding 101. End of line!


All Transcripts posts