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 = Time.now + number * 3600
"The time in #{number} hours will be #{time.strftime('%I:%M %p')}"
end

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.

More: http://apidock.com/ruby/String/to_i

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

http://localhost:4567/bill

then

params[:name]

are “bill”

Example:

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

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

http://localhost:4567/BILL

You will get

Hi there BILL!

If you request

http://localhost:4567/what/time/is/it/in/2/hours

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

Sources: http://stackoverflow.com/questions/6885990/rails-params-explained

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.

How to Back Up WordPress Manually, Without Plugins

backup cloud

backup cloudWhile there are many backup solutions both paid and free some of you will want to know how to backup WordPress site or blog manually without relying on plugins or paid services. It is a matter of preference really, but if you are a control freak like me or if you have too much to lose in case of disaster, knowing how to do this will definitely not hurt.

What you have to know about your WordPress site is that ir consists of:

  • database that can be accessed via PHPMyAdmin in your cPanel. Database contains all your pages, text, titles, plugin settings, menus, ads etc.
  • WordPress files with wp-content folder being the most important. This can be accessed via File Manager in your cPanel. wp-content folder is important because it contains all your images, themes, plugins. Obviously images here is what we are most concerned about.

What it means is you can afford to lose all the rest, because it can be easily restored with fresh WordPress install. Even your themes and plugins aren’t typically something to worry about because you can easily download and install them again, but your text and images are crucial.

So basically, if you back up just these two you are fine. The disasters in form of being hacked or database failure never stroke me yet but I had to use my backup quite a few times because I like to switch my hosts and when I do so I prefer to transfer my files myself (remember, I am a control freak).

The good news is that manual backup is very easy to do. Some of you might find using FileZilla a little challenging for wp-content backup, but even your great grandma and elementary school kids can download database backup, provided your host gives you a cPanel.

Most hosts will provide you a cPanel. I used Hostgator, Site5, StableHost, Siteground, ASmallOrange, Bluehost… it all works there. Bluehost and Site5 looked just a little different but the principle is the same and it’s very easy to figure out how to do it there.

So let’s just do it!

Step 1.
First get rid of anything you have in your dashboard that you don’t use. Now you don’t have to do this but it’s a good habit to keep things lean and light. If you keep twenty twelve, twenty thirteen, twenty fourteen themes when you use Genesis or any other theme it is a waste. Same goes for plugins. The more themes and plugins you have the longer you will have to wait for your wp-content to download. Then most probably you will be uploading it to Dropbox or similar service where after you pass a certain amount of storage, you will have to upgrade to a paid account or a more expensive account. The leaner, the better.

Step 2.
Go to your cPanel by entering in your browser

yourwebsite.com/cpanel

and when you see this

login with your username and password (those of your web host, NOT WordPress).

Scroll down and find Backup Wizard and click to enter:

cPanel backup wizard

Click on backup

backup button

Under Select Partial Backup chose Backup MySQL Databases

mysql database backup

Click on that and you will see the list of databases you have. Most WordPress sites have only one so it will be a no-brainer what to click on. Click on the name of your database to download it to your computer.

As you noticed there were other options to backup email forwarders, filters and home directory but I personally never do this. I barely use my email so I am not worried to lose it. For home directory, if you only have a WordPress install, I find it wasteful to backup everything. As I said earlier you can have a fresh install of WordPress, the only thing that matters there is wp-content folder, so to save my time and space at file storage company (Dropbox in my case) I only backup what is absolutely necessary.

Step 3.
Now you need to open FileZilla. If you don’t have it installed go ahead and download it and install it now from here. FileZilla is a free software that allows you to connect to remote server where your website files are located and to your local computer at the same time. Its convenient interface allows you to navigate both, download, upload, delete and rearrange files as you please. Best of all, it allows you do to do things in bulk so you can hit the button and go have your coffee while it works for you.

I will assume you know nothing about FileZilla and will explain how to configure it first.

So open your FileZilla. Left window represents your local computer, you can navigate your folders right from here. If you created a special folder where you want to save your backup navigate there, or else just choose Desktop or Downloads folder. In my case, I chose Desktop.

filezilla desktop

Now we want to connect to server to be able to see all our website’s files in the right window which is currently empty. We need to connect first so, if it’s not done yet, you need to create a new project by clicking File ==> Site Manager ==> New Site

Give a name to your new site project. It can be anything you want, not necessary site name. As long as you recognize it, anything will do. Then configure access as shown. For most hosts entering mysite.com in Host field will work, provided that your site is already hosted there and name servers propagated. You might meet an occasional host that will not work that way and will use IP address or name server instead. In that case enter IP address provided by your host or name server. If nothing seems to work or if in doubt, contact them and ask what works specifically with their server.

Switch Logon Type to Normal, enter your username and password (same as cPanel) and click Connect. Filezilla will remember these settings, so when you do this next time you will not have to worry about the settings.

Here is the screenshot how it should look like when you are about to click connect.

Filezilla settings

If you entered correct login details and host name your directory listing should be successful. You will also notice that you have some files in the right window. This is what you have on your server. If you were to go to File Manager via cPanel you would see them there, but Filezilla interface is just another way to see them and manage them. Double click on public_html to see your website’s files. When working here be careful not to drag or rearrange files accidentally, because they are all drag-able and rearrange-able.

Once again you will see “Directory listing successful”. In the right window you will see even more files (those that were inside your public_html). Scroll down to find

wp-content

Right click on wp-content and choose download. It will take a while to download so you can just leave your computer alone.

wp-content backup with filezilla

When it will be done you should find downloaded backup wherever you chose to save it (in my case it’s Desktop). You can now zip it and send it to your file storage place together with database (database is already compressed).

You are done!

I never keep very old backups, so when I do new one I delete previous backup so I can stay within free account limit in Dropbox. You can save more than one if you wish, but my point is you don’t have to keep all backups you ever do unless you want them.

More on FileZilla here.