MacMost Now 885: Using Ruby On Your Mac

Ruby is a popular programming language that comes installed on your Mac and can be accessed in the Terminal. You can use the Rub command line interpreter to run simple programs in a single line. You can also write more complex programs in a text file and run them. This gives you similar functionality to using BASIC on the Apple II.
Video Transcript / Captions
Closed captioning for this video is available on YouTube: MacMost Now 885: Using Ruby On Your Mac.

Hi this is Gary with MacMost Now. On today's episode let's take a look at how to create short little programs on your Mac using Ruby.

Now if you have been using computers for a long time you probably remember back when, say on your Apple II, you could type in a programming command and get a response. Or just type in a simple basic program and run it. Well you can't really do that with modern computers with Macs and Windows' machines. You have to use some sort of app to create a program. But Mountain Lion has a program called Ruby built in that you can access through the Terminal window and for me it is very reminiscent of how you used to use Basic on an Apple II.

So I have just run the terminal app here to open a simple terminal window and I'm going to type in Command irb which will launch the Ruby interpreter that is installed with Mountain Lion. You can see I get a new prompt there and I can start typing Ruby commands.

Right away we can type some simple math in and get a result there. So you see I can use this as a calculator. You can put in fairly complex things maybe more so than you usually do using a calculator or the Spotlight menu.

Now you can also go and use variables. So I can do say a=6, b=7, and get the result of a*b. But you can do even more than this. You can do something like loops for instance. So Ruby loop might look like this. I'm going to use a ; to fit everything on one line. I'm going to puts to output here and I'm going to do i*2 so I should get multiples of 2 and "end" and the loop. You can see there that it runs my program there and gives me the output, 0,2,4,6,8,10, and then it gives the output for the total thing which you can ignore here, it is the return value of this line. In this case it is this that we are interested in.

You can also even get into arrays. For instance I can create an array like this of just some names and it puts that value in there. Then I can do a for loop, put a space there, and you see I get this string typed out three times with the different values there.

I may want to create an array an easier way like this and then you split and now notice this. If I use the up arrow I can go back to previous lines without typing them. So I can jump back to this line right here and hit return. It just saves me the time of having to type this again and I get new output because I've changed the value of "a".

Now if you are not a programmer you might not understand what it is I am doing here. But you can learn Ruby pretty easily. Just search for Ruby tutorials and there are tons, I mean dozens or even hundreds of quick little tutorials and ruby is a very easy language and a good language if you haven't programmed before. So you may want to go through some of those tutorials and after you get the hang of things then you can go back and use irb to try out some commands.

Now I can also enter Ruby commands into a program file. So I can create a program I can easily reuse. I am going to use the nano text editor. You can use vi or anything you want as a text editor. Or even just use TextEdit or an app like Textwrangler or BBEdit to create these and then run them in terminal.

So I'm just going to create example.rb and it is just a blank file here. I'm going to create an array of numbers and then a simple for loop. puts to output everything double and end. So it is a simple set of commands and I can exit and save it example.rb and I use ruby and then example.rb to run it. You can see it runs it and I can run it again. So I can edit it now, change the values, things like that.

Here is something that is slightly more useful. I'll set some input, for example2.rb and I'll set a = to the results of get string. Strip off the return character has to be done and converted to an integer and then I will put the output of that times three. This is a simple program here. If I were run it it is going to prompt me for a number. I'll do 7 and you can see it returns 3 times 7.

So really just scratching the surface here. My main point is to show you this Ruby interpreter and that you can create short little Ruby programs on you Mac very easily. So just look at some tutorials and learn a little Ruby if you want to learn how to write some quick programs on your Mac.

Hope you found this useful. Until next time this is Gary with MacMost Now.

Comments: 6 Responses to “MacMost Now 885: Using Ruby On Your Mac”

    6 years ago

    Thanks for the Ruby video. I missed the old Apple II access to Basic. Looks like Ruby interpreter might fill the void.

    6 years ago

    This is great! I made a simple phone book which shows telephone numbers when typing in a name, or the person’s name when typing in a phone number (for those times when you have that proverbial slip of paper with no name on it). Also good as an e-mail list.

    6 years ago

    Interesting video Gary. I didn’t know Ruby was built into the terminal. Before I sit down and learn Ruby, could you give me some examples of some practical uses where Ruby can be used? I have some C programming experience but have never encountered Ruby. What could I use it for?

      6 years ago

      Hard to answer your question — it is a programming language like any other. It is like asking what can you use C for? It depends what environment you are programming in and what you are trying to do. I’d imagine that with the right tools, you could do anything. But it doesn’t mean it is the best or right tool for a specific job. There are plenty of Ruby resources online for you to check out.

        6 years ago

        Thanks for your thoughts Gary. I’ll have a look around the net.

    6 years ago

    Thanks for the video. I had no idea that it was built-in to the Mt. Lion terminal.

Comments Closed.