Ruby vs Python

Ruby vs Python: The ultimate battle

A tough question that goes through the mind of all web developers. Ruby vs Python. Which one is better? Both of them are popular languages suited for developing web applications. In short, it’s the clash of the Titans.

The Language

Python is inspired by languages like C/C++, Java, Perl, Lisp, and ICON. Python believes in “only one best way to do a particular thing”, thus being strict on indentation and layout, even the amount of whitespace to use. Therefore Python is said to be easy to learn and extremely readable.

Ruby is influenced by languages like C, Java, Perl, and C++. Ruby believes in “language’s code should always cause as little confusion as possible for the developer.” Ruby makes use of blocks or first-class objects. A block is treated as a unit within the program, resulting in the language to be extremely potent, combining with the use of lambdas and functional programming. Additionally, Ruby takes the OOP concept to a great extent.

The Frameworks

Ruby on Rails is the popular web framework built using the Ruby programming language while the most popular web framework built using the Python programming language is Django.

The Similarity

Being high-level, object-oriented scripting languages, both Python and Ruby have similar performance. They both come with standard libraries, an interactive shell, and persistence support and are excellent choices for web development.

The Differences

 Ruby and Python take a different approach to solve problems.

Ruby is flexible and empowers the programmers. Ruby on Rails allows the developers to perform many little tricks to make an elegant web framework. The flexibility seems to be magical at times but this could even do harm at times. Sometimes code works when you didn’t expect it to and leaves you feeling really impressed. Other times the tracking down bugs could be hard and will cost you hours.

Python follows a more direct approach to programming. The aim is to ensure that everything is clear to the programmer. Python loses the elegance that Ruby gives but makes it easier to learn the code and debug problems.

Ruby showcases the flexibility of the language while Python focuses on the directness and readability of the code. Ruby focusses on giving the programmers the freedom to adopt the approach they want. Python emphasis on the ease of use and learning in a single right way of doing something. As a result, Python developers are conservative in their approach and the developments and new features of Django are added slowly. On the other hand, Ruby developers enjoy the freedom and change they get resulting in the Rails framework to undergo constant changes.

The Community Support

Both Python and Ruby both have huge community support, influencing in the direction of the language, updates, and the way software is built.

Python has a much more diverse community than Ruby as there are many academic use cases in both Math and Science that Python resulting in the support in those areas. Python is also installed on almost every Linux systems making it the perfect language for server usage.

Ruby’s started to gain popularity since Rails came out in 2005. The Ruby community grows quickly around Rails and focused on web development. The community gets more diverse with time, but it does not have the same diversity that Python has.

Major Users

Python has been used by many companies including Google, Pinterest, Instagram, National Geographic, Mozilla Firefox, and the Washington Post.

Ruby on Rails is the popular web framework built using the Ruby programming language while the most popular web framework built using the Python programming language is Django.

Ruby has been used by companies like Apple, Twitter, Airbnb, Shopify, Hulu, Github, and Groupon.

The Verdict

It’s still hard to point out which language is the better one. Each language has preferred areas where they are better, due to the features and support they provide. If you are into developing web applications, go with Ruby on Rails. They have a very strong community who are always on the bleeding edge. If you are interested in developing web applications but would also like to learn the language more generally, focusing on academic and scientific programming, go for Python and Django. They have a diverse community, influenced and supported by the various industries.

Jekyll Docker image missing C headers

So, if you are using Jekyll Docker image and you need to install any Ruby gems that require C headers, such as raindrops in my case, which was a dependency for unicorn, you will find yourself faced with this error:

fatal error: asm/types.h: No such file or directory

Fortunately, there is a quick fix for this. Jekyll docker image allows you to add system dependencies very easily. You just need to mention the dependent packages inside a file called .apk at the root of your project or beside Dockerfile. Since the docker image is based out of Alpine linux, you need to specify the correct package name as in Alpine packages.

Create a file called .apk and add the following line to it.

linux-headers

Now, when you bring up your containers, it will automatically install the linux-headers package which contains all the required header files and proceed to installing the gems successfully.

Custom Table View Cell using Interface Builder with RubyMotion

custom-cell-sample-app

I’m a RubyMotion newbie and I wanted to create a custom table view cell which simply had three labels – a title, a subtitle and a third label on the right side of the cell. I found a few help around for creating custom table view cell programmatically, which helped me do the job, but it didn’t look as nice as I wanted. Moreover, the benefits of using Interface Builder to easily layout and style the labels was something I was looking for. Fortunately, I was able to get around it myself after fiddling about it. Thought I’d share it for those who are looking for similar.

This post assumes you already have a table view controller in your project to which you want to add a custom table view cell. So I won’t go into much detail of adding them.

I use ib gem to use Interface Builder with RubyMotion. If you don’t, I highly recommend you do. My aim is to add a custom cell with three labels inside a table view controller, call it BooksController.

Create the table view controller

Create the table view cell class

Build the interface

Now, open Interface Builder using the rake ib command in Terminal from the root of your project. The ib gem will automatically create the appropriate interfaces and properties for you to be used inside IB.

Set Custom class for the table view controller in IB

Assuming you have a table view controller added to your storyboard, choose the table view controller and open Identity inspector on the right. Set the class to your controller name – “BooksController” for me.

Set datasource and delegate of the table view controller

Perhaps this is already done for you or drag them from Outlets inspector to the controller itself.

Set Custom class for cell

Now choose the table view cell on the storyboard and set the class to the class you created – CustomBookCell in my case.

Set Identifier for cell

In the Attributes inspector, set the Identifier of the cell to the identifier you specified in the code – BookCell for me.

Set tableview cell Style to Custom

In the Identity inspector, set the Style to Custom from the drop down.

Add three Labels to cell

Now add the three labels we need on the cell by dragging them from the Object library.

Style the labels as necessary

Feel free to make them look good.

Add auto layout constraints

Once the labels are in place, it is a good idea to set the auto layout constraints. This is easy as pie with IB and another great reason to use IB. Simply select the three labels together and choose from menu Editor -> Resolve Auto Layout Issues -> Reset to Suggested Constraints.

Drag outlets from Connections inspector to the labels as required

Finally, open Connections inspector and now you should see the outlets you created in your custom cell class under Outlets. Drag them to their respective labels.

Save and quit Interface Builder and fire up the app in simulator. You should now see the table view with your custom cell.

A sample app for this is available on Github.

Using Hstore in Rails 4 with PostgreSQL

Hstore basically gives you a schema-less data store in your PostgreSQL DB. This allows you to store the Hash in a Database column.

If you want to query Model records, it is very difficult if you use Serialize to store dynamic attributes. Hstore acts same as Serialize but we can additionally query the model records using Hstore.

Please follow below steps to enable Hstore in a Ruby on Rails 4 application.

rails generate migration enable_hstore_extension
class EnableHstoreExtension < ActiveRecord::Migration
  def change
    enable_extension 'hstore'
  end
end
&#91;/ruby&#93;

<pre>rake db:migrate</pre>

<pre>rails g migration AddPropertiesToChocolate</pre>


class AddPropertiesToChocolate < ActiveRecord::Migration
  def change
    add_column :chocolates, :properties, :hstore
  end
end
&#91;/ruby&#93;

<pre>rake db:migrate</pre>

<strong>Rails console:</strong>


p = Chocolate.last
p.properties = {'color' =&gt; 'orange'}
p.properties['price'] = '5'
p.save
Chocolate.where("properties -&gt; 'color' = 'orange' ")
Chocolate.where("properties -&gt; 'price' ='5' ")

Finally, you can define accessors for your Hstore keys in your model. Validations work just like they would for any other column in your model.

class Chocolate &lt; ActiveRecord::Base
  # setup hstore
  store_accessor :properties, :color

  # can even run typical validations on hstore fields
  validates :color,
  inclusion: { in: %w{orange gold red} }

end

An Introduction to Web Crawling using Ruby and Nokogiri

Web Crawling

A web crawler is a program automated script that browses world wide web in methodical and automated manner.Key motivation for designing web crawlers has been retrieve web pages and add their representations to local repository.   Other less frequently used names of a web crawler are

  • Bot
  • ants
  • automatic indexers
  • worm

Difference between web-crawling and web-scraping

Web scraping,  is the process of processing a web document and extracting information out of it.

web scraping will focus on two things:

  1. Examining what the webpage expects from the user and what it shows the user.
  2. Processing the data being sent or received by the browser

Web crawling, is the process of iteratively finding and fetching web links starting from a list of seed URL’s. Strictly speaking, to do web crawling, we to do some degree of web scraping (to extract the URL’s.)

Next we discus  the theory, technique, and programming needed to write web-scrapers.

Use of web inspector in crawling

All of the major browsers have a web inspector built-in or available to them.This highlights selected elements.Data-seekers will get even more utility out of the network panel, which provides a way to directly examine the data and logic underneath the webpage displaying.

The network panel is used to  examine the source of dynamically loaded data requests like Javascript or Flash.

Nokogiri (Rubygem)

RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries.

Nokogiri  is an HTML, XML, SAX, and Reader parser. Among Nokogiri’s many features is the ability to search documents via XPath or CSS3 selectors.

Features of Nokogiri

  •    XPath support for document searching
  •  CSS3 selector support for document searching
  • XML/HTML builder

Parsing HTML with Nokogiri

Step -1 require rubygems and nokogiri pakaege

All web crawling script is having  these two lines in the beginning

require “rubygems”

require “nokogiri”

Step – 2  Opening page with open-uri pakage.

If the webpage is stored as a file on  hard drive, pass it in like so:
page = Nokogiri::HTML(open(“index.html”))

If the webpage is live on a remote site, like  http://en.wikipedia.org/

page = Nokogiri::HTML(open(“http://en.wikipedia.org/”))

If the webpage is live on a remote site, like http://en.wikipedia.org/, then include the open-uri module, which is part of the standard Ruby distribution but must be explicitly required’

ie.  require “open-uri”

Open-uri  encapsulate all the work of making a HTTP request into the open method, making the operation as simple as as opening a file on our own hard drive.

Step – 3  Selecting elements

Nokogiri’s css method allow  to target individual or groups of HTML methods using CSS selectors.
Eg.
page = Nokogiri::HTML(open(“index.html”))
puts page.class   # => Nokogiri::HTML::Document
page.css(“title”).text #=>title of the web page

The css method does not return the text of the target element,
It returns an array – more specifically, a Nokogiri data object that is a collectino of Nokogiri::XML::Element objects. These Element objects have a variety of methods.
Eg
text
name
attr
value etc..

Page.css(“csspath”)[:atrname]   #=>atrvalue
Page.css(“a.link”)[:href]        #=>”http://anyvalue.come”

Parsing XML using Nokogiri

There is small difference in xml parsing with html parsing . In xml parsing use XML class for parsing .

Page = Nokogiri::XML(open(“file.xml or url”))
retuns an Nokogiri::XML::Document
Use object method “xpath” like “css” method for
to target elements
Page.xpath(“copy xpath from browser inspector”)

we can use all other object method described above for xml parsing