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
Ruby Method, Proc, and Block Confusion
Converting Time in Utc to Pacific Time
Access Slack Files from a Slack Bot
How to Send Mail with Ruby Over Smtp with Ssl (Not with Rails, No Tls for Gmail)
How to "Nest" the Inclusion of Modules When Using the Ruby on Rails Activesupport::Concern Feature
Ruby Block and Returning Something from Block
Fresh Install of Rvm in Ubuntu Isn't Letting Me Install Gems (Zlib Error)
Where Do You Put CSS Files in a Rails App Directory
How to Daemonize Rails Rake Task on Elastic Beanstalk Start Up
After Ruby 2.4 Upgrade - Error While Trying to Load The Gem 'Uglifier' (Bundler::Gemrequireerror)
How to Do Fuzzy Substring Matching in Ruby
Alter Rails Params Hash from Rack Middleware
Generating Sequential Numbers in Multi-User Saas Application
Elegant Command-Parsing in an Oop-Based Text Game
Rails Sitemap_Generator Uninitialized Constant
Implicit Argument Passing of Super from Method Defined by Define_Method() Is Not Supported