[MUSIC] In this section of the course, we'll start using the Ruby programming language to study programming languages. So in this first segment, I want to do a mix of discussing various Ruby logistics. And also giving you some background on why we're using Ruby, and why it's a good choice for this portion of the course. So to start with logistics, the main website for Ruby, you can see here, is at ruby-lang.org. The installation instructions for what you need to do to get Ruby running on your computer are available on the course website. We suggest different things for different operating systems, and so on. In the lectures, you'll see that I like to use the Emacs editor for writing Ruby programs, and I encourage you to do the same. But you're welcome to use any editor, and indeed, many editors have plenty of support for Ruby that will be more than enough for our purposes. There are several different versions of Ruby available, and so let me say a little bit about versions. And in fact, if you are watching carefully, you've probably noticed that this portion of the video was spliced in. Because I made the mistake in the past of mentioning specific versions by name that are now no longer widely available and pre-installed in different operating systems and what not. So none of the intellectual concepts we're going to consider depend on an exact version of Ruby. The language is fairly stable. But nonetheless, we want to support a range of versions, in particular because there are often times where certain versions are easy to install on a particular operating system and others are not. So you'll see in the software installation information sort of what versions we're currently supporting, and instructions that should be easy for you to get one of those supported versions installed. I just want to emphasize that the exact version really doesn't matter. In fact, all of the code we're providing is unlikely to depend on version. Your code for the program assignments will probably work fine for any recent version of Ruby. But just in case you accidentally rely on some slight change between versions, we want to be very clear about what version we're supporting. Now, because we're already in part C of the course and we've got more savvy at picking up our own languages, we may be a little less careful in this portion of the course in explaining every last detail of the language features that we're considering. But we do intend for everything to be self contained and provide everything that you need for the homework. Now, there's wonderful free documentation for Ruby available defining the language, particularly giving information about the very large standard libraries. And you may need to consult that a little bit. But again, we're not going to send you on a lot of hunting for library methods that you may not know about. If you do want a book that provides more information about Ruby and how people program in Ruby, I made a recommendation here on the slide that I found particularly helpful. But this should not be necessary. This is an additional resource if you'd like to learn more or would like that sort of book format. The materials for the course, as we have all along, should provide you what you need. So let me focus a little bit on why we're studying Ruby, what's different about this language, and why it fits with I want to cover in the rest of the course. The biggest thing is that it is a pure object-oriented language. What I mean is that all the values in the language are objects. There is nothing but objects. Even numbers are objects. So that will help us to study object-oriented programming. It's also class-based, but what I mean by that, and we'll explain this in subsequent segments, is that we define classes. And then every object has one particular class that says how that object behaves. So if you've seen Java or C#, or a very OOP take to C++, this is similar. There are object-oriented programming languages that are not class based. The best known today is JavaScript. And I wanted to stick with a class-based language and explain things in those terms. We'll also see that Ruby has a very nice feature called Mixins, which are a little bit like Java interfaces and a little bit like C++ multiple inheritance. But they overcome some of the limitations of each, and that'll be a nice thing to study. Ruby is a dynamically typed language, just like we saw Racket was dynamically typed. I want to show you another one. And I also feel that will help us study OOP, object-oriented programming, not to have the types in our way right from the beginning. There are some other things about Ruby we'll study. Its convenient support for reflection, finding things out about objects at runtime, the fact that it's very dynamic. The fact that it has closures, just like we've grown to love in ML and Racket. And lastly, that it's a scripting language. Now, there is no exact definition of what makes a language a scripting language. But we'll see various things that are unusual when you've seen other languages. Things like when you need a new local variable, you can just assign to it anywhere in your method, which is like a function, without having to say first that you want to have something in that scope that has that variable name. So there's a lot more to Ruby that we are not going to focus on. And I'll be honest, that some of these things are what makes Ruby such a popular language. But they're less germane to our study of the concepts of programming languages. Ruby has lots of support for manipulating strings, and we will not study much of that support. Ruby is very popular for writing certain aspects of server-side web application using the Ruby on Rails framework, which we will not discuss. And in general, Ruby has an attitude of, why not support a big language with lots of different ways to do things very conveniently. And we will by no stretch of the imagination cover the entire language. We'll just focus on a subset of the features that will let us study object-oriented programming, and then a few other things as they become relevant along the way. So this will complete for us our third position in this little chart you see at the top. That after we've studied Ruby, we'll have seen a dynamically typed functional language and a dynamically typed object-oriented language. We've also seen a statically typed functional language. And many of you will have already seen a statically typed object-oriented programming language, such as Java or C#. So it's nice to fill in that table. And I should be clear that we didn't have to use Ruby for this. It turns out that Racket also has classes and objects, should you want to do object-oriented programming in Racket, for example. But Ruby makes a more complete commitment to them. In Ruby, everything is an object, and so it's nice to switch to such a language. It's certainly not the only language where everything is an object. The language Smalltalk actually has a lot in common with Ruby. It's just a much older language, it's actually a much smaller language. So from a teaching standpoint, it's nice to use, because I could teach you the entire language of Smalltalk. On the other hand, it's less modern and less practical for modern applications, and so I'm happy to use Ruby. So a final logistical note before I show you a simple program and move on to the core material in this section. Which is, the homework for this section of the course is actually a little bit different. What we decided to do is, instead of, like the other homeworks where you pretty much wrote all of the code and maybe we give you a little bit of code to get you started. The way this homework works is, we're actually going to give you something in Ruby that's a few hundred lines and completely works already. And you just need to make various extensions without modifying the code that's already there. So this is actually a good way to learn a language, which is just to read some code already written in it. The challenge of this homework assignment is actually mostly not on writing your code, but on reading the code that's already provided to you. I think this is a very common skill in software development. We're often modifying programs or enhancing programs that are already been written. And it's a great way to learn a new language. So we're going to try that. It's a different approach, and I think you'll enjoy it. The program is actually graphical, so part of the installation instructions is making sure that you can use the Tk graphics library with Ruby. You don't need to understand Tk very much at all, but you will need to have it on your system. And that'll get us set up for the homework. So with that as background, let me just finish up by showing you a small Ruby program. As I mentioned, I'll use Emacs, but you can use another editor if you like. Emacs should be already configured to handle Ruby quite well by default. If you just open any file with a .rb extension, hopefully you will see Ruby down here on the bottom line, suggesting that we'll get all the indentation and syntax color highlighting that we want for the Ruby language. And then this is the code, which we'll understand more after another video or two. Comments start with the hash character and go until the end of the line. Things are generally organized into classes, so I've defined a class Hello here. It has a method called my_first_method. Methods are a lot like functions in ML or Racket, but we'll understand their differences soon enough. And then the body of this method is just puts Hello, world! puts is a built-in method that takes a string and prints it out for you when you run the program. Out here at the top level, I've defined a variable x that creates an instance of the Hello object. And then I call it to my_first_method method. And none of that needs to make any sense to you yet. I'm just showing you here a program, so that you can see me using the tools. And for those of you who do know a little Ruby, or all of you will soon, there are much shorter ways to write a program that prints Hello, world! But I wanted to show you some of the key pieces. Now we do have REPL for Ruby, but I'm not going to show you using it inside of Emacs. Instead, I'm going to use it over here in a terminal. To open this on Windows, I just ran this cmd program, the command prompt, and that got me here. I changed into the directory where my Ruby files were. And if I just wanted to run this program, I can just take the Ruby program which I've already installed and pass it the name of my file. And you'll notice that it prints out Hello, world and then gives me my prompt back. Now I did promise you a REPL. There's a program called IRB for interactive Ruby. Hit Return, and I get a read eval-print-loop prompt, just like you expect. 3+4 evaluates to 7. By the way, don't say semicolon at the end. If you do that, it'll be waiting for another expression. And so I have to say something like 5, and then I'll get 5 back as my answer. If I want to load the contents of a file, that's load and then in quotation marks, "ruby_intro.rb". This is like Use when we use ML, or the Run button when we used Racket. Hit Return, we'll see it printed Hello, world! And the result I got back was true, because that was the result returned by the puts method. So here I am at my prompt. I could continue to program in Ruby for as long as I wanted, and then I was done I could hit quit, and that would leave the REPL. And that's our introduction to Ruby. We'll jump into the details of the language in the next segment.