# Beginner Ruby Question

**URL:** <https://www.ruby-forum.com/t/beginner-ruby-question/168258>\
**Category:** Ruby\
**Created:** [May 18, 2009, 8:31pm UTC](https://www.ruby-forum.com/t/beginner-ruby-question/168258 "2009-05-18T20:31:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![calvin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f475e1/32.png) [@calvin](https://www.ruby-forum.com/u/calvin)\
**Post date:** [May 18, 2009, 8:31pm UTC](https://www.ruby-forum.com/t/beginner-ruby-question/168258/1 "2009-05-18T20:31:07Z")

</div>

Hi,

I just intstalled Ruby 1.9 on my mac with macports. The installation  
was successful but when I type in ruby -v at the command prompt, Ruby  
1.8.6 shows up still. The latest Pickaxe book says I have to have /opt/  
local/bin in my path to add it… but don’t really get how that’s  
supposed to work.

Thanks for your time,

Calvin

---

<div class="post-metadata">

**Author:** ![calvin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f475e1/32.png) [@calvin](https://www.ruby-forum.com/u/calvin)\
**Post date:** [May 18, 2009, 8:41pm UTC](https://www.ruby-forum.com/t/beginner-ruby-question/168258/2 "2009-05-18T20:41:23Z")

</div>

Calvin wrote:

> I just intstalled Ruby 1.9 on my mac with macports. The installation  
> was successful but when I type in ruby -v at the command prompt, Ruby  
> 1.8.6 shows up still. The latest Pickaxe book says I have to have /opt/  
> local/bin in my path to add it… but don’t really get how that’s  
> supposed to work.

You should be able to use your new ruby by its full path name:

$ /opt/local/bin/ruby --version  
ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-darwin9]

To make that the Head Ruby In Charge, try this:

echo ‘export PATH="/opt/local/bin:$PATH"’ \>\> ~/.profile  
export PATH="/opt/local/bin:$PATH"  
hash -r

You should then see the right ruby by default:

$ which ruby  
/opt/local/bin/ruby

---

<div class="post-metadata">

**Author:** ![calvin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f475e1/32.png) [@calvin](https://www.ruby-forum.com/u/calvin)\
**Post date:** [May 18, 2009, 10:27pm UTC](https://www.ruby-forum.com/t/beginner-ruby-question/168258/3 "2009-05-18T22:27:11Z")

</div>

On May 18, 11:36 am, Jeff S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-darwin9]  
> /opt/local/bin/ruby  
> I tried it out but I am having trouble

when I type in: echo ‘export PATH=“/opt/local/bin:$PATH”’ \>\>  
~/.profile  
nothing happens

if I type in: echo ‘export PATH=“/opt/local/bin:$PATH”’

i get: export PATH=“/opt/local/bin:$PATH” on the terminal screen

and when i type in: hash -r  
the terminal says -r isn’t valid

Would you be able to break things down into simpler steps or describe  
what you type in and in what order… i know you already kinda did  
that but I am new to using the terminal and would appreciate it alot.

Thanks again for your time… sorry for bothering you about this stuff  
but [hivelogic.com](http://hivelogic.com) is down today for some reason.

---

<div class="post-metadata">

**Author:** ![calvin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f475e1/32.png) [@calvin](https://www.ruby-forum.com/u/calvin)\
**Post date:** [May 18, 2009, 10:52pm UTC](https://www.ruby-forum.com/t/beginner-ruby-question/168258/4 "2009-05-18T22:52:00Z")

</div>

echo ‘export PATH="/opt/local/bin:$PATH"’ \>\> ~/.profile

adds ‘export PATH="/opt/local/bin:$PATH"’ to the end of your  
~/.profile which probably is where your shell setup info is stored.  
try looking at ~/.profile – the export PATH stuff should be at the  
end of the file.  
Adding it to ~/.profile adds /opt/local/bin to the path on login, and  
the ‘hash -r’ should have reevaluated the ~/.profile and added the  
changes.

If you just want to try it without all that, you could just do

export PATH="/opt/local/bin:$PATH"

which will add /opt/local/bin to the path.  
then

ruby -v

should be 1.9 assuming the binary is in /opt/local/bin.

Nick

---

<div class="post-metadata">

**Author:** ![calvin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f475e1/32.png) [@calvin](https://www.ruby-forum.com/u/calvin)\
**Post date:** [May 18, 2009, 11:16pm UTC](https://www.ruby-forum.com/t/beginner-ruby-question/168258/5 "2009-05-18T23:16:15Z")

</div>

Calvin wrote:

> On May 18, 11:36 am, Jeff S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Calvin wrote:
> > 
> > > I just intstalled Ruby 1.9 on my mac with macports. The installation  
> > > was successful but when I type in ruby -v at the command prompt, Ruby  
> > > 1.8.6 shows up still. The latest Pickaxe book says I have to have /opt/  
> > > local/bin in my path to add it… but don’t really get how that’s  
> > > supposed to work.

> when I type in: echo ‘export PATH=“/opt/local/bin:$PATH”’ \>\>  
> ~/.profile  
> nothing happens

You’re not supposed to see anything happen, except that the shell should  
prompt you for the next command.

I’m assuming you’re using “bash” (Bourne Again SHell), the default  
command-line shell on Mac OS X (and most Linux distros). Since “hash  
-r” is not working, you may not be using bash, after all. The next most  
likely candidate is called tcsh. What do you see when you run this?

```
 echo $SHELL

```

It’s also traditional for different shells to use different prompts.  
Bash uses $, csh uses %, and tcsh uses % or \>. As a special case, the  
root user’s prompt is always set to the comment character (#), to avoid  
accidental copy/paste mishaps.

When bash sees

```
 echo 'export PATH="/opt/local/bin:$PATH" >> ~/.profile

```

it does a several things. One is to expand the tilde (~) to the path of  
your home directory. Another is to recognize “echo” as a built-in  
command, and execute it. The command’s standard output is redirected  
(\>\>) to the end of the .profile, and echo’s command line arguments are  
exactly two:

```
 echo
 export PATH="/opt/local/bin:$PATH"

```

Be careful not to mix up \>\> with a single \>, which would redirect echo’s  
standard output to overwrite the file entirely.

The bash command:

```
 export PATH="/opt/local/bin:$PATH"

```

prefixes “/opt/local/bin:” to the value of the PATH environment  
variable, and marks that variable as “exported” so its value will be  
inherited by subprocesses. The equivalent command in tcsh is:

```
 setenv PATH "/opt/local/bin:$PATH"

```

The PATH variable holds a colon-separated list of directories to check  
for the programs you invoke. For example, when you type ruby, the shell  
should find /opt/local/bin/ruby. By contrast, if you were to type  
xeyes, the shell would check for /opt/local/bin/xeyes, but then have to  
keep looking through PATH (since xeyes lives in /usr/X11/bin).

The point of setting PATH in ~/.profile is that bash reads .profile when  
you “log in.” Logging in, in this context, just means starting a new  
terminal. You also want to set it for the current shell, so you end up  
enter the export command twice: Once as an argument to echo

> > ~/.profile, and again at the current shell’s prompt.

When bash finds the location of a program, it may cache that location,  
to make future look-ups faster. If you change PATH, it’s a good idea to  
make bash throw away its cache, and look up program locations anew. The  
command to make bash discard its cache is

```
 hash -r

```

The equivalent for tcsh is

```
 rehash

```

> if I type in: echo ‘export PATH=“/opt/local/bin:$PATH”’
> 
> i get: export PATH=“/opt/local/bin:$PATH” on the terminal screen

To the shell, double quotes (") mean “group these things into a single  
command-line argument, but expand any environment variables.” Single  
quotes (') perform the grouping, but also prevent expansion. You need  
single quotes for the echo to .profile, because you want $PATH to be  
expanded when the shell

For example, given the following commands:

```
 hello='What is up'
 echo $hello world
 echo "$hello world"
 echo '$hello world'

```

echo will be invoked three times, each with a different set of  
arguments. In the first case, the two arguments will be ‘What is up’  
and ‘world’ (without the quotes). The double space will be lost, and  
echo will just print a single space between its arguments. In the  
second case, echo will receive the single argument ‘What is up world’  
(without quotes), with the variable expanded and the double space  
preserved. In the third case, echo will receive the literal text  
‘$hello world’ (again, no quotes), exactly as it was entered on the  
command line.

> and when i type in: hash -r  
> the terminal says -r isn’t valid

That’s a little odd. What happens when you type rehash?

---

<div class="post-metadata">

**Author:** ![calvin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f475e1/32.png) [@calvin](https://www.ruby-forum.com/u/calvin)\
**Post date:** [May 19, 2009, 12:31am UTC](https://www.ruby-forum.com/t/beginner-ruby-question/168258/6 "2009-05-19T00:31:14Z")

</div>

On May 18, 2:13 pm, Jeff S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > nothing happens
> 
> your home directory. Another is to recognize “echo” as a built-in  
> The bash command:  
> for the programs you invoke. For example, when you type ruby, the shell  
> When bash finds the location of a program, it may cache that location,
> 
> > if I type in: echo ‘export PATH=“/opt/local/bin:$PATH”’
> 
> (without quotes), with the variable expanded and the double space  
> preserved. In the third case, echo will receive the literal text  
> ‘$hello world’ (again, no quotes), exactly as it was entered on the  
> command line.
> 
> > and when i type in: hash -r  
> > the terminal says -r isn’t valid
> 
> That’s a little odd. What happens when you type rehash?

Oh- I am using bash and i think the hash -r thing worked (i think)

when i type in: which ruby1.9 I get /opt/local/bin/ruby1.9  
and when I type ruby1.9 -v I get ruby 1.9.1p129

but

if I just type in: which ruby I get /usr/bin/ruby

and if I type ruby -v I get ruby 1.8.6

Thanks so much again for helping me out Jeff

---

<div class="post-metadata">

**Author:** ![calvin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f475e1/32.png) [@calvin](https://www.ruby-forum.com/u/calvin)\
**Post date:** [May 19, 2009, 5:40am UTC](https://www.ruby-forum.com/t/beginner-ruby-question/168258/7 "2009-05-19T05:40:47Z")

</div>

Calvin wrote:

> > > ~/.profile
> > 
> > your home directory. Another is to recognize “echo” as a built-in  
> > The bash command:  
> > for the programs you invoke. For example, when you type ruby, the shell  
> > When bash finds the location of a program, it may cache that location,
> > 
> > > if I type in: echo ‘export PATH=“/opt/local/bin:$PATH”’  
> > > echo $hello world  
> > > ‘$hello world’ (again, no quotes), exactly as it was entered on the
> 
> but
> 
> if I just type in: which ruby I get /usr/bin/ruby
> 
> and if I type ruby -v I get ruby 1.8.6
> 
> Thanks so much again for helping me out Jeff

Do you have /opt/local/bin/ruby, or only /opt/local/bin/ruby1.9? To  
have port install ruby1.9 without the suffix, you have to pass the  
+nosuffix switch:

```
 sudo port install ruby19 +nosuffix

```

Here’s the thread I followed when I was installing ruby 1.9 on OS X:  
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/0beb1ceb68a5ab3f](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/0beb1ceb68a5ab3f)
