Terminal Not Executing Ruby Files

Rookie - Ruby: Run file in terminal

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.

Why won't Terminal execute Ruby files?

1+2 just evaluates 1+2. Unless you output the result, it simply get swallowed.

Change your 1+2 to puts 1+2 and your script will output 3.



Related Topics



Leave a reply



Submit