to_i Method in Ruby

Q: What to_i stands for in Ruby and what it does?
A: to_i stands for “to integer” and is needed to convert strings into integers so you can do mathematical operations with them. Any value, even numbers, acquired from URLs in params becomes a string and if you need to do some math with it you need to convert it to integer.

get '/what/time/is/it/in/:number/hours' do
number = params[:number].to_i
time = + number * 3600
"The time in #{number} hours will be #{time.strftime('%I:%M %p')}"

The number you enter will become a string, to calculate time and give correct answer you need to make it integer. That’s where to_i comes in.


What are Params in Ruby

Q: What are params in Ruby?

A: params in Ruby are equivalent of $_REQUEST array in PHP. They can come from:

  • query string of GET request,
  • input from POST request,
  • or the path of the URL.

If user requested




are “bill”


require 'sinatra'
get '/:name' do
name = params[:name]
"Hi there #{name}!"

will return value depending on user’s input. If you enter


You will get

Hi there BILL!

If you request


params id was set to “2” and so on.


How to Restart Sinatra Automatically

Here is the solution that will save you time and nerves while working in Sinatra / Ruby. To reload Sinatra automatically, in the beginning of your document right after the opening

require 'sinatra'

add this:

require 'sinatra/reloader' if development?

You can now see changes by simply reloading page in your browser, no need to type anything in your terminal or Windows PowerShell to restart the server.

Add Facebook Comments in PHP Includes

facebook logo small

Facebook comments should include URL of each individual page or they won’t work properly. This can be a real pain if you are managing a static website, because you have to generate and paste code for each individual page manually. You could include their code in your normal includes of course but then ALL comments will appear on ALL pages, which is not a solution.

If your pages process PHP or if you can force them to process PHP as I described in this tutorial, you can as well include your Facebook comments in includes and everything will work properly. The code to include (this is for include itself, let’s call it “comments.php”):

$currentlink = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
echo "<fb:comments href='$currentlink' num_posts='2' width='500'?></fb:comments>";

You can edit number of posts and width if necessary.

You should place a trigger on your pages as follows

<?php include 'includes/comments.php'; ?>

where “includes” is the folder that contains your include files (if you actually have it) and “comments.php” is the name of your include where you pasted the code above. Needless to say you could give it a different name ;-). You should paste this trigger to where you want your comments to appear. This of course has to be done manually for each page but once it’s done everything will be automated.

The comment box will display comments only for each individual page. You will be then able to moderate them at Facebook Developers Tools provided that you followed their instructions and set up an app for your Facebook comments moderation.

I learned this trick from Don Caprio’s post here. He also goes into more details on how to set up Facebook app for comment moderation, just in case you somehow missed it.