This beginners guide will help you setup ruby programming language on ubuntu linux (or any other debian distro). I am using instruction that applies for debian subdistros and ubuntu, so If you’re using any other than debian tree distros, See if you can find similar instructions for the installation. You can skip the installation part If you’re using any other linux distribution.
Installation
There are multiple ways to approach the ruby installation. In case of ubuntu you get community maintained version of ruby in software center or apt-get. You can also install latest version of ruby from the repository. Follow the instructions to install ruby environment.
Step 1:
sudo apt-get update
This will update us with the new package sources.
Step 2:
sudo apt-get install build-essential git-core curl
These are the packages which are necessary to install as ruby rvm depends on these packages.
Step 3:
sudo apt-get ruby-rvm
This step installs rvm which makes it easy for us to install ruby.
You can now either install ruby via rvm or you can use the following command.
sudo apt-get install ruby1.9.1 ruby1.9.1-dev rubygems1.9.1 irb1.9.1 ri1.9.1 rdoc1.9.1 build-essential libopenssl-ruby1.9.1 libssl-dev zlib1g-dev
That’s it once the installation is done, you are free to use ruby on terminal. If you’re using previous version of ruby pre-installed with your distro, you can force rvm to use specific version like this:
rvm --default use 1.9.3-p194 //just an example
Ruby Basics
Let’s start with some basics of ruby on terminal.
Type the following to open ruby interpreter in terminal.
irb
This will open the ruby interpreter where you can run or type your command.
Clear the Ruby terminal
system"clear"
This command will clear the previous screen output and will present the clear terminal.
Running Ruby scripts
You can run the ruby scripts by using some IDE or you can run them on command line. In case of command line you have to navigate to the directory where there is the file that you wish to run.
ruby demo.rb
Here we are running the demo.rb script. All we have to do is call the ruby interpreter.
Hello Ruby
Let’s start with our first interaction on the terminal.
Type the following.
puts " Hello Ruby"
This will output hello ruby in return. You can also use print “hello ruby”.
Ruby String Input and Output
Let’s see if we can get input from the keyboard.
puts "Where is new york?"
city=gets()
puts "New york is in- " +city+ "."
This will show us the location or country name for the new york city in output. Try it and see the results. If it becomes difficult for you to type it in one line with semicolon, you can always run this as a file in terminal.
That’s it. This completes our simple tutorial on using ruby on ubuntu linux. In next tutorial chapters we’ll see how to work with strings, loop, methods and regex. I’ll update this page with links to upcoming tutorials in future.