Writing Scripts in Ruby on a Mac

I have learned to do very basic commands with Codecademy and Why’s Poignant Guide to Ruby, but, more importantly, this week I learned how to write, save, and run scripts in Ruby. It was actually surprisingly difficult to figure it out, since nearly every source I combed through online just said to save a script with .rb, then type “ruby filename.rb” in terminal. With the lack of additional information, I did not know where I should write a script, what format it should be in, etc. It was only after a lot of trial and error and one very helpful video on youtube that I understood what needed to be done. This post will take you through the steps to write a ruby script and run in it the terminal.

1. Open Terminal. Type “pwd” to see which directory the terminal is currently on. The directory is a folder in your computer. As you can see from the screenshot below, I am currently in the folder /Users/student. I can either use that folder, or create a new one that is specifically for my programs. I recommend the latter.

2. To create a new folder from the terminal, type “mkdir myruby”. myruby is the name I have chosen for my folder; yours can be anything you want it to be. If you open Finder, there is now a new folder named myruby. (mine says File exists because I already have a myruby folder) However, the terminal is still in /Users/student. To change the directory to your new folder, type in terminal “cd myruby”.  Now the terminal has access to that folder and can pull files from it.

3. Now for actually creating a script. First, open TextEdit or TextWrangler. I prefer TextWrangler because it automatically highlights your code after saving, which can be helpful in reading it. However, either will work.

4. Write your code. I used the classic “puts “Hello world!”” as my piece of sample code. You can of course put anything you like.

You may notice that mine is already saved and thus highlighted. After you finish writing your code, simply give it a name and append .rb onto the end of the name, like so: “hello.rb”. Make sure to save it in the folder you created earlier (mine was myruby).

5. Now that you have your saved code and ready-to-go terminal (unless you closed it, in which case use cd myruby or whatever name you chose to get back to your folder), you can run your script in the terminal. Simply type “ruby hello.rb” into the terminal to run the script.

6. All done! To make more scripts, just write and save them in the same folder. Most importantly, always type “cd foldername” to ensure terminal has access to the right folder and therefore to your scripts.

Let others know too...Email this to someoneShare on FacebookTweet about this on TwitterShare on LinkedInShare on Google+Share on Tumblr

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>