Kevin Archer
Kevin Archer

Reputation: 27

Rookie - Ruby: Run file in terminal

Very inexperienced Ruby student here.

I’m not sure how to write a Ruby program in Atom, save it then try to run it in the terminal (Mac OSX).

Could someone run me through the absolute basics, please?

Forgive my cluelessness!

Upvotes: 1

Views: 935

Answers (1)

oheydrew
oheydrew

Reputation: 36

Ruby programs generally use the '.rb' extension, so in order to run a ruby file that you've written, you need to save it somewhere with that extension first- eg. 'my-app.rb'.

It's a good idea when starting out to save it in a folder inside your "Home" directory (/Users/your user name/). You can find that in the mac "Finder" by clicking on the folder on the left hand list that's named "your username". In your terminal, your home directory is shortened to '~/' - and you can easily change directory into it with that shortcut:

cd ~

While I've been learning, I've stuck to a quick, short directory to store my files- '~/code/'. Anything will do, but it's much quicker to type 'cd ~/code/my-app.rb' than to type something long like 'cd ~/Documents/Programming/Ruby/my-app.rb' every time. So when you're deciding on where to save, think about how much you'll have to type in terminal! :)

Once you've saved your file, and used 'cd' to change into the directory you've saved it in, you use the command 'ruby' to run it.

ruby my-app.rb

That's about all there is to actually running your file! There's so much more to using the terminal, and writing code- but there's plenty of info out there on how to start.

I found Chris Pine's "Learn To Program" really simple and easy to follow. There are plenty of other resources out there, too! Try out Try Ruby to get going straight in your browser.

Upvotes: 1

Related Questions