Ruby is a dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax (similar to Python syntax) that is natural to read and easy to write.
In this article, you'll learn how to setup the development environment of Ruby in Windows easily.
Download a Ruby distribution for windows
You'll need to install a Ruby distribution for Windows in your system. You can download the most recent version not in the official website, but in rubyinstaller.org in the downloads area. This is a self-contained Windows-based installer that includes the Ruby language, an execution environment, important documentation, and more.
If you don’t know what version to install and you’re getting started with Ruby, we recommend you use Ruby 2.2.X installers. These provide a stable language and a extensive list of packages (gems) that are compatible and updated. However, not all packages (gems) are maintained. Some older packages may not be compatible with newer versions of Ruby and RubyInstaller.
Install the distribution
Now execute the installer and proceed with the installation of Ruby for windows as you usually do with any installer.
However, in the Installation Destination and Optional tasks do not forget to check all three options in order to use ruby easily on your computer.
Proceed to install and wait till the wizard finishes.
Finally you'll be able to run Ruby code and use its tools in Windows easily !
Hello world
You can execute Ruby code now in your computer, so let's write some code. Before continue, check in your command prompt (cmd.exe) if you've succesfully installed ruby executing the following command on it:
ruby -v
The environment variable ruby should be available everywhere in your console, and the previous command should output something similar (with the version of your distribution) in the console:
Now get started with your Hello World in Ruby, create a file named hello.rb
wherever you want. In this case we are going to create a folder in the Desktop with the name ruby and we are going to save the hello.rb
file inside.
The previously created file will contain the following code:
puts 'Hello world ! The date and time now is : ' + Time.now.strftime("%d/%m/%Y %H:%M")
To execute a ruby file, you only need to do "ruby path-to-the-file-and-filename
" and you're ready to go. As mentioned previously, in this example the file will be located in the ruby folder in the desktop, so you can navigate to the folder using console commands and then execute the file:
cd C:\Users\sdkca\Desktop\ruby
ruby hello.rb
Or just provide the absolute path of the file as second parameter:
ruby C:\Users\sdkca\Desktop\ruby\hello.rb
And the output of the console will be:
And that's all, simple isn't? Now that you're able to execute Ruby code in your machine, we recommend you to visit the "Give Ruby a Try in your browser" website, to get familiar with the most simple tasks that this programming language have to offer you. Have fun !