Ruby is a dynamic and a true Object oriented language.Due to which ruby has a lot of ways to compare two objects.To be exact it has 4 different ways to compare objects that I am aware of.To understand Ruby objects and equality comparison works lets create a class.
Since I have been playing a bunch of iPad games.Lets create a game object
Lets get some game instances to work with.Now comparing with the == operator.
As you can see creating two game object with the same title and comparing them gives you a false result.
This is because the == comparator compares the object’s identity.To get the objects identity.Send the object_id message to it.
Now comparing with the equal? operator.
The equal? operator works the same way as the == operator.But unlike the == operator the equal? operator must never be overwritten as it is used internally by ruby to compare object identity.

The eql? operator
The eql operator works a bit differently compared the above two comparators.It compares the hashes between two objects.Even though the final result tends to be similar to ==. Lastly we have the === operator.
The triple equal operator is used in case statement comparison.

So now which operator do I use and when.
1. Double Equal operator:According to ruby conventions, if you want to compare two objects which you own ,you can override the == operator to make it compare properties in your object.

class Game
  attr_reader :title
  def initialize(title)
    @title = title
  end
  def ==(other)
    other.is_a?(self.class) && @title == other.title
  end
end

 

When you try to compare 1 == 1.0 you will get true because ruby casts 1 as to a Numeric class and then compares with the 1.0.

2. Triple Equal operator: Like double equal you can use the triple equal operator to compare two objects and also override it to get your desired result.

3.equal?: The equal? operator is used by ruby so use it carefully and never override it.

4.eql?: Like the equal? operator it is better not to override the eql operator and use it to compare both the class and equality of two objects.



Elixir is a ruby like language built on top of the Erlnag VM.It has some really nice features which makes writing recursive functions a breeze.Here is a defacto example to write a factorial program in elixir.

As we can see having guards and pattern matching makes recursive functions a breeze

Comparing that too ruby,the LOC are more but the code is more readable.

Comparing it to some ugly javascript.



error
Most of us have seen this dreaded error page when developing on rails.Launching up irb or rails console wastes a lot of time and brain cycles.
Having said that I came across this neat gem called ‘web-console’.This gem is really handy ,it launches rails console right there is the browser along with the current context.
Installing it is pretty simple Add it to your Gemfile and you are up and running.Make sure you have the version mentioned below or something higher.

gem 'web-console', '2.0.0.beta2'

 

web-console



This post came out during the Angelhackdubai hackathon.More importantly the angelhack team promised to give away a prize for the person who would tweet the most mentioning #angelhackdubai @thecribb on their twitter feed.

Connecting to twitter was made possible us the twitter gem.The gem doesnt take care of the oauth communication with the new twitter 1.1 and above api.It requires you to do the oauth dance provide it the required keys and tokens.

All I had to do is mention my:

  1. Consumer Key
  2. Consumer Secret
  3. Access Token
  4. Access Token secret


Writing the following was a quickie but the getting some content for tweet was important as we need to tweet something reasonable.
This brought me to the unirest gem.It is an http post/get request library.
After a quick googling I came across ICNDB.com which is website with collection of jokes from Chuck Norris.
The site also includes api to make make request programmatically.GET request to http://api.icndb.com/jokes/random would give me a random joke.
Here is the final result



Continuing with previous post,this post will cover whats new in Rails 4.1 .
Most of my information comes from the blog post on the rails blog.This is my take on the new features and will mostly cover the ones useful for me in my day to day development.

To speed up our development start time and test ,rails preloader has now been replaced with spring, which works just like spork.It creates a separate process which loads the rails environment in memory thereby reducing time for running our test as rake reloads the rails environment every time we run a rake task.

I have been using the env gem to store my secret_keys for apis like facebook,aws..etc.Rails now has the secrets.yml in the config folder.We can now store all the configuration keys in the secret.yml file.We can retrieve the key using the Rails.application.secrets.{secret_name}.

ActiveRecord Enums is another important feature.Even though it is something trivial to implement.Rails makes it easier to store stat of a particular model to a particular integer.Plus it creates many handy methods which is provides to query.

 

There are many more changes ,to learn about them visit this link.

 



You must be wondering why I am writing a blog about Rails 4 even thought it came out nearly 6 months ago.This is mainly because I found some new features and missed some old ones.

One of the most obvious upgrades in rails 4 is that rails now requires ruby 1.9.3+.Which is really great as it helped clean up the core of rails.But as a developer the first change I had to make to my code Strong Parameters.

  • Strong Parameters re-enforces security in by only permitting certain variables through the params field.
  • Turbolinks:Turbolinks is a feature on rails which makes use of javascript to speed up the loading time of the website.The way it works is during the first load of the website.It loads the initial page and takes control over the links.Every time on would click on the link it would make a ajax call in the background and replace the content in the body of the html page.Thereby reducing the requests made to the server and making the website much snappy.This sounds good in theory but in practice this breaks a lot of websites as it has it own onclick and onload events which could break old code.
  • Postgres HStore:Using hstore in postgres has become even easier with the native extension for hstore data.We start with enabling the extenstion in the migration.


    We create the attr table in the images table as hstore datatype.

  • ActiveModel is another important feature in the rails where you can use regular models in rails without the persistence layer.We can even use validations just like a ActiveRecord::Base model.

 



With one of our products in our company we were given the task of building a file uploading system which would take different type of files and upload it to our server.Instead of creating a separate class for each file and having different methods to upload the file, we instead opted for a creating a polymorphic class which can be used with different classes with the has_many relation.

First we begin by creating a migration with the polymorphic relations in the database

rails g migration CreateAssetsTable

We then fill in the migration

class CreateAssetsTable < ActiveRecord::Migration
  def change
    create_table :assets do |t|
      t.string :type
      t.references :attachable, polymorphic: true
      t.timestamps
    end
  end
end

We then run the migration

rake db:migrate

This sets up the assets table with 6 columns.Id,Type,attachable_id,attachable_type,created_at and updated_at.The important columns for the polymorphic relation are the attachable_type and attachable_id.This sets up the relation between other classes and the asset class at the database level.
Next we create the model for this class

rails g model asset

This will create the model class and set us up with an empty model class.We then will in the polymorphic relation.

class Asset < ActiveRecord::Base
  belongs_to :attachable, polymorphic: true
end

Just like in the migration the polymorphic relation is the most important feature of this class.

Now that we have the asset class ready we then create other class which will use this relation.
Example:

class Users < ActiveRecord::Base
   has_many :avatars, as: :attachable, class_name: "Asset"
end

The has many sets up the has_many relation with the user model.So now the user has many avatars which refers to the assets model for the asset with type avatar.
Now that we have dealt with the polymorphic nature of the classes,It becomes very easy to add the upload module to the asset class using the paperclip gem by thoughtbot.
We add the gem to the gem file

gem 'paperclip'
bundle install -j4

Next we generate migration for the assets table to hold your new data

class AddAttachmentToAsset < ActiveRecord::Base
  def up
    add_attachment :assets, :file
  end
  def down
    remove_attachment :assets, :file
  end
end
rake db:migrate

We the add the has_attached_file to the Asset class

class Asset < ActiveRecord::Base
  belongs_to :attachable, polymorphic: true
  has_attached_file :file
end

Since version 4 of paperclip content validation is required by default.
So we also add a simple image file validation using regex

   validates_attachment_content_type :file, content_type: /Aimage/.*z/

This check if the mime type of the file contains image/ followed by the format.
Now we have a generic file upload class.



Writing a rake task in rails i pretty simple.In this post i will describe creating a rake task to reset the password of every user on the system.Maybe you had a breach and to prevent identity theft you reset the users password and send it to the user through his email address.

To create a rake task we firstly have to namespace it so that it doesnt conflict with existing rake tasks and it also helps us keep our code more modularized.

/// lib/tasks/defcon.rake
require 'securerandom'
namespace :defcon do
  desc 'Reset users emails'
  task :got_hacked do
    begin
      User.all.each do |user|
        password = SecureRandom.hex.slice(0,10)
        user.password = password
        user.save
        UserMailer.reset_password(user,password)
      end
    rescue Exception => e
      puts "#{e.message} occured"
    end
  end
end

We first require the securerandom library.This is required to generate some random password for each user.
We namespace the task with a name,for now i have called defcon.
The task name here is got_hacked which runs once the we type rake defcon.got_hacked so if in case we get hacked we can instantly reset the password for all the users in our system.
Inside the task we have begin and rescue block,this is make sure that we catch any exception that occurs and output it on the screen.
So the regular flow of the program is as such.We loop through all the users in the system (assuming there is a user model).We generate a random password and set that as the password for the current user in the loop.We save the password in the database and then send them a email stating that their password has been reset.
The UserMailer class for sending the email is given below

/// app/mailer/user_mailer.rb
class UserMailer < ActionMailer::Base
  def reset_password(user,password)
    mail(to: user.email, subject: "We got hacked", from: "[email protected]")
  end
end

The view for the email

/// app/view/user_mailer/reset_password.html.erb
Dear <%= user.name %>,
We have been hacked.Here is you new password for the account.
password : <%= user.password %>