Sunday, May 11, 2014

Pluck vs. map and select

Just a quick look at pluck vs. map and select in Rails.
Lets say we have a User with an active and a balance field on him. The active field is a boolean and the balance field is float. Now, we want to select the balance of all our active users. We can of course do this in several ways. We start by defining a scope for finding the active users:
scope :active, -> { where active: true }
Now we can grab all active users with:
User.active
We want to find each their balance, so something like this might come natural to most:
User.active.map(&:balance)
The SQL generated here will actually be:
SELECT "users".* 
FROM "users" 
WHERE "users"."active" = 't'
This means that we will grab every field on the User model and instantiate that object, and then, with .map(&:balance) grab the balance from each object.
We want to do all this in the SQL, as we are just tossing all the other data, we do not need to actually fetch it from the database. We could do something like this, to not get all the unneeded fields:
User.active.select(:balance).map(&:balance)
This will generate this SQL instead:
SELECT "users"."balance"
FROM "users" 
WHERE "users"."active" = 't'
and then only provide us with dummy User objects with the balances. Mapping over these, will yield the exact same result as above, however this time, we do not have to toss any data as we only fetch the balances.
Oh, but we mentioned balance twice in the above ruby code. That's not very nice, can't we have some sugar for that? Well, of course we can.pluck to the rescue.
To do a combined select and map we can do:
User.active.pluck(:balance)
which yield the exact same SQL:
SELECT "users"."balance"
FROM "users" 
WHERE "users"."active" = 't'
We'll end up with an array of balances, but this time we only had to tell it once about the balance field.
Conclusion: Always use pluck in favour of select and then map.

Switch to New Ruby Hash Syntax

Try and run this in your terminal:
find . -name \*.\*rb -exec perl -p -i -e 's/([^:]):(\w+)\s?(\s*)=>\s?(\s*)/\1\2: \3\4/g' {} \;

When working on old Ruby code, you'll see the old (<= 1.9) hash syntax:
{
  :name     => "Mads",
  :age      => "25",
  :position => "Lead developer"
}
However, often in new projects you'll want to use the new:
{
  name:     "Mads",
  age:      "25",
  position: "Lead developer"
}

Saturday, July 30, 2011

Expanxion :

Please send the details of the students in the following format

Please send the details of the students in the following format -
Name DOB Score in 10th Board Score in 12th Board Score in Graduation Field University Score in MCA Sem1 Score in MCA Sem2 Score in MCA Sem3 Score in MCA Sem4 Certifications Experience Technology Interested in
(Java/.Net/QA)
Number of BackLog(if any)
 

As well send the resume, it should include following details.
 
MCA Details
SemXX
Subject Name
Marks Obtained
Theory
Internals
A


B


C


D


Project Name
Technology Used
Overview
 
Experience (if any)
Certifications (if any)
Any other projects done apart from curriculum