3

I just started reading the Well-Grounded Rubyist, and I am just beginning to use Ruby in my terminal on my Mac.

I'm on the very first lesson, creating a Celsius to Farenheit converter in a text editor. I've saved the code as an .rb file by using Textmate (my text editor). The file name is c2f.rb. The file is saved in a folder on my desktop titled "Rubycode".

I am having difficulty running the .rb file in the terminal however. I've tried many different methods of trying to call the file, including using:

cd /Users/rexrose/Desktop/Rubycode/c2f

and many others.

Any thoughts on what exactly, I'm supposed to type into terminal in order to call the c2f file?

Thanks in advance.

1
  • @LoïcFaure-Lacroix: You should make that an answer. Commented Aug 4, 2013 at 2:04

2 Answers 2

7

I just started reading Well-Grounded Rubyist.

That's a very good book. I consider it more of an intermediate level book than a beginner book, but no matter.

I've tried many different methods of trying to call the file, including using

cd /Users/rexrose/Desktop/Rubycode/c2f

The cd command means "change directories" and you cannot change directories to a file. Instead, you have to change directories to the directory containing the file:

$ cd /Users/rexrose/Desktop/Rubycode

Then you can execute your program contained in the file c2f.rb like this:

$ ruby c2f.rb

Here are some Terminal tips:

1) You can use ~ instead of /Users/YourUserName, so you can save some typing by doing this:

$ cd ~/Desktop/Rubycode

Typing '~' instead of '/Users/YourUserName' will become second nature.

2) Using the cd command with no arguments:

$ cd

will take you to your home directory, i.e. /Users/YourUserName

3) You should change your prompt to indicate what directory you are currently in. To do that, create a file called .bash_profile in your home directory(/Users/YourUserName). Check to see if it exists first:

$ cd
$ ls -al

The command ls -al will show all the files in a directory, including hidden files, which are files whose name begins with a .. If a file named .bash_profile exists, open it; if it doesn't exist, create it. Put this in .bash_profile:

PS1="\w$ "

To get Terminal to recognize the changes, you can either Quit Terminal and relaunch it, or do this:

$ source .bash_profile

Then open a new Terminal widow.

You can also add 'aliases' to .bash_profile. For instance, in my .bash_profile I have the alias 'r' for 'ruby', so that I can execute a ruby program like this:

$ r my_program.rb

In .bash_profile you make an alias like this:

alias r="ruby"

4) Tab completion in Terminal:

You might have noticed that you can type part of a file name, then hit tab and Terminal will complete the file name. Using tab completion, I can execute my ruby program like this:

$ r my_pr<tab>

In fact, I name my practice ruby programs so that I can use tab completion to the greatest effect. I have files named 1.rb, 2.rb, 3.rb, and then I execute one of them by simply typing:

$ r 1<tab>

And in fact, you may not even have to type that! If you hit the up arrow key on your keyboard, Terminal will display the previous command, and if you hit the up arrow key again, you will see the command before that. So you can scroll up to a previous command, then hit return to execute it--without having to type anything.

You should endeavor to use tab completion for each of the file names in a path. For example, if you are cd'ing to /Users/YourUserName/dir1/dir2, you should do this:

$ cd /Use<tab>/YourUser<tab>/di<tab>/di<tab>

The reason you should use tab completion for each filename(by the way in Unix filename is a general term for both directory names and file names) is because when the name won't tab complete, then you are in the wrong directory or you are trying a filename that doesn't exist in that directory. So instead of having to type out the whole path '/Users/YourUserName/dir1/dir2' and then finding out about the error when you hit return, the tab completion will let you know immediately when there is an error(because the filename won't tab complete)--saving you some typing.

5) Because you will probably be using Terminal for mostly ruby programs for awhile, you can set up things so that Terminal will automatically open up in your directory Users/rexrose/Desktop/Rubycode. Put this in .bash_profile:

cd "/Users/rexrose/Desktop/Rubycode"  (Here you cannot use ~)

6) Occasionally, you may have to type a long file name that exists on your computer into the command line:

$ cd /Library/SomeLongName/AnotherLongName34832o222/142582dir/some_file.txt

Instead of having to type all that at the command line, you can locate the file in Finder first. Then if you drag the file onto the Terminal window, the file name will be entered at the point of the cursor.

Finally, a better way to organize your files might be to put them in directories below your home directory, like this:

~$ mkdir ruby_programs
~$ cd ruby_programs
~/ruby_programs$ mate 1.rb
Sign up to request clarification or add additional context in comments.

Comments

1

First things first: cd stands for "Change directory".

Normally the terminal should open in "~", which is the home directory where most of your things are. In OS X it will be /Users/[username]. It's also possible possible that in OS X, it will save the location of the last session. I also recommend, since you're starting to install, "Iterm2", which is a nice terminal to use. It supports multiple tabs, etc.

Ruby, the interpreter, is the command "ruby". To call a script you have to call Ruby with a filename:

ruby /Users/rexrose/Desktop/Rubycode/c2f/c2f.rb

That is almost the equivalent of:

cd /Users/rexrose/Desktop/Rubycode/c2f/
ruby c2f.rb

It's almost equivalent, but for now the difference shouldn't bother you. Let say that the second way to call the script is more favorable than the first.

Now, the second thing: If you want to try things in Ruby, you can start an interactive shell. The command is "irb".

Type irb and Enter and then you can type Ruby code. If you want to leave, press CTRL+C multiple times.

The last thing, I recommend installing "RVM". It will save you time and pain, I hope. If you want to install Ruby gems, it will not mess with the Ruby already present with the system. That's my personal opinion but I believe lots of people will agree. Even if Ruby comes with OS X, you should install a different Ruby for development. It will make sure that if something goes wrong in dev, it will not mess the Ruby OS X might be using.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.