Tuesday, October 20, 2015

Installing Ruby on Windows | Running Ruby basic app with Sinatra

Fam,

Yesterday was a very challenging day with trying to run a "basic" application in Ruby on Sinatra (local ruby server - I believe). However, I ran into quite a few issues because all of the examples I was looking at were using either a Mac or Linux. Well, that's nice - and very typical of a programmer. But, what about the millions who use Windows? I thought Ruby was independent of platform?

Anyhow, here's my new attempt to get the "basic" application in Ruby running on Sinatra on my local to continue using GitHub's Basic Authentication tutorial.

So, I'm NOT going to use Cygwin with Ruby anymore. This was probably one of the core issues. I strongly advise not wasting a whole day with figuring out how to run Ruby stuff in Cygwin.

Installing Ruby on Windows

I already installed Ruby with the quick installer and placed in my C: drive. Notice that Ruby developers advise NOT to install in a location where the path has spaces (e.g. "C:/Program Files"). I also already created a System Environment Variable called RUBY_HOME pointing to the Ruby location on C: drive and added this variable to the PATH.


Oh wait! After reading this following message on the Ruby Installer site, I'm going to uninstall Ruby 2.2.3 (x64) and install Ruby 2.2.3 (32bits) version.

The 64-bit versions of Ruby are relatively new on the Windows area and not all the packages have been updated to be compatible with it. To use this version you will require some knowledge about compilers and solving dependency issues, which might be too complicated if you just want to play with the language.

I am installing with all options. And then I click Finish.



I update my RUBY_HOME environment variable. I also notice that my User environment variable has Path to Ruby\bin location and there are extension pointing to it when a file extension recognized with *.rb or *.rbw. This must be from selecting the option labeled "Associate .rb and .rbw files with this Ruby installation.".

Here's my new command output of:  ruby -v


Now, I'm going to download the corresponding Development Kit for use with Ruby 2.0 and above (32bits version only). If you don't see it, then try scrolling down the page to see this section on Development Kit. Here's the webpage with instructions on this kit and its associated wiki.

When I run this, the program installer asks to extract to a location. So, I'm extracting to my "C:\Ruby22_Dev_Kit" folder (which I just created).

I'm following the instructions on the wiki, section "Quick start".


According to wiki's section "5. Test Installation", I'm going to see if my Ruby environment is correctly installed. Notice that I'm still using Windows Command Prompt to run these commands. I didn't see any instructions on using MingW or MSYS strangely. Well, that's because it's already included in the Ruby directory and Ruby uses the MinGW compiler.


Pay attention to the advice and instructions in the wiki's section "Example Native RubyGem Installations using the DevKit". I'd highlight "it's crucial that you include the --platform=ruby option to force RubyGems to build the native gem...".


That's it - victory!


Quick Test of Ruby (using IRB)

Let's test our Ruby installation with some basic commands. I'll leverage the quick tutorial by Ruby @ https://www.ruby-lang.org/en/documentation/quickstart/

IRB - Interactive Ruby

Now the tutorial says "If you're using Windows, open Interactive Ruby from the Ruby section of your Start Menu. BUT, you can open IRB from the command line using the following command:

>irb


That's it - victory!

Running Ruby basic app with Sinatra

Let's test running Sinatra on our machine now that we have Ruby properly installed. I'll be using this reference: http://www.sinatrarb.com/

Before I begin, I just want to make sure the server is not started or installed or anything with a simple call in the browser to Sinatra's default port:  localhost:4567



I created a directory for storing my Ruby Projects where I'll store my first Sinatra project.
Location looks like "C:\Users\[me]\RubyProjects\hi"

Based on the homepage of the Sinatra site, I'm adding the code into a pipe (i.e. file) called "hi.rb" and storing this file in folder ".../RubyProject/hi". Now I'm going to smoke it and see what happens.

1. Install the RubyGem "Sinatra" and remember to add the argument "--platform=ruby" as instructed by the RubyInstaller wiki (from above).



2. Navigate to the Ruby directory containing the "hi.rb" file and run the Ruby file.  > ruby hi.rb


We won't see the INFO response statements until we complete the next step. But, this is a good initial response so far.

3. Go back to the browser and refresh to see Sinatra running (per the 'require' statement in the ruby file) the "hi.rb" file. This should display a simple "Hello World" statement in the browser. But, we didn't get this. Instead, Sinatra kindly tells us what to code to try in order to see what we're expecting.



Let's update the code in our file "hi.rb" according to Sinatra's "try this" advice and then restart Sinatra by doing entering the following commands with focus in Command Prompt window:
Ctrl+C  (pressing these keys simultaneously to stop the Sinatra server from running)
>ruby hi.rb






Yes!, success!

That's it! In closing this practice, here's my code in the "hi.rb" file.

require 'sinatra'

get '/' do
  "Hello world, it's #{Time.now} at the server!"

end

This is why I have a little more than a simple "Hello World!" in my browser display. I hope this was helpful for you like this was for me.

God Speed,
G2

No comments:

Post a Comment