Blog @ RohitRox

tips tricks tuts and everything learned

Ruby Dynamic Methods

| Comments

We define methods using def keywords which is fine for most of the cases.

But consider a situation where you have to create a series of methods all of which have the same basic structure and logic then it seems repititive and not dry.

Ruby, being a dynamic language, you can create methods on the fly.

So, what does that mean?

lets see this simplest example:

1
2
3
4
5
6
7
8
9
10
11
12
  class A
    define_method :a do
      puts "hello"
    end

    define_method :greeting do |message|
      puts message
    end
  end

  A.new.a #=> hello
  A.new.greeting 'Ram ram' #=> Ram ram

Jquery Performance Tuning

| Comments

After searching and digging a lot of artciles about improving jquery performance in web apps, I decided to make a list of best common performance tips and bect practices. We’ll also reveal some of JQuery’s hidden feature and how we can use them for performance tuning.

Lets divide this post into 4 main categoris.

  • Selector Performace
  • DOM Manipulation
  • Events
  • Digging into JQuery Secrets
  • General and All-Time performance tricks

Feature Flippers With Rollout

| Comments

Rollout gem comes handy when we need deploy a beta feature, may be to a selected group of users or some percentage of uses so that few users can try it out before it goes massive.

So lets rollout.

First of all, make sure that you have redis, because rollout uses redis as its backend. Then add gem 'rollout' to gemfile and bundle install.

Now, we need to create a rollout setup file inside initializer.

Let’s name it rollout_init.rb which looks like this:

1
2
3
4
5
6

  $redis = Redis.new
  $rollout = Rollout.new($redis)
  $rollout.define_group(:admin) do |user|
    user.admin?
  end

Documentation suggests to use a global variables. So we create one with a new redis instance. Then, we create rollout instance passsing that redis instance. We can configure rollout in many ways, here we are defining an admin group by passing a block and checking whether the user belongs to that group or not. Complete documentation can be found here.

Next, we can now use handy rollout method like for example let’s say we need to activate chat feature for admin group only.

1
2
3
4
5
  if $rollout.active? :chat, current_user

    ....

  end

VIM Quick Tips

| Comments

Vim quick tips and tricks, listing some useful, essential and most often used Vim commands.

Moving Around

Command Action
j or Up Arrow Move the cursor up one line.
k or Down Arrow Down one line.
h or Left Arrow Left one character.
l or Right Arrow Right one character.
e To the end of a word.
E To the end of a whitespace-delimited word.
b To the beginning of a word.
B To the beginning of a whitespace-delimited word.
0 To the beginning of a line.
$ To the end of a line.
H To the first line of the screen.
M To the middle line of the screen.
L To the the last line of the screen.
:n Jump to line number n. For example, to jump to line 42, you’d type :42
gg Goto start of file
G Goto end of file
I go to the beginning of the line and go into insert
A go to the end of the line and go into insert
Ctrl+g Show file info including your position in the file
i Insert at cursor
a Append after cursor
A Append at end of line
ESC Terminate insert mode
u Undo last change
U Undo all changes to entire line
dd Delete line
3dd Delete 3 lines.
D Delete contents of line after cursor

MySql Commands

| Comments

This is a short-list of handy MySQL commands that I have to use time and time again, in my development machine as well as in servers and for importing and exporting databases.

Login to mysql console (from unix bash)

1
2
3
$ mysql -u user -p
# if host name is required
$ mysql -h hostname -u user -p

Create a new database

1
mysql> create database databasename;

List all databases

1
mysql> show databases;

OOP With Javascript

| Comments

JavaScript has strong object-oriented programming capabilities, even though some debates have taken place due to the differences in object-oriented JavaScript compared to other languages.

Objects and Classes

JavaScript is a prototype-based language which contains no class statement, such as is found in C++ or Ruby. This is sometimes confusing for programmers accustomed to languages with a class statement. Instead, JavaScript uses functions as classes. Defining a class is as easy as defining a function. In the example below we define a new class called Person.

1
function Person() { }

To create a new instance of an object new operator.

1
my_person = new Person();

Javascript Static and Instance Methods

| Comments

Custom JavaScript objects can have instance methods (function that are associated with a particular JavaScript object), but like other Object-Oriented languages, they can also have static methods, that is functions that are associated with the JavaScript class that created an object, as opposed to the object itself. This is useful in cases where a function (a.k.a. a method) will not be different in different object instances.

ActiveRecord With Ruby Alone

| Comments

Active Record is the main tool that Rails developers use to communicate with and underlying database. Active Record does some wonderful things for a web developer looking for abstractions in database setup, SQL connections and queries. We can get a cool command line Ruby application or utility script for our daily tasks using Active Record going in about 5 minutes!

First of all, get the activerecord gem using

1
gem install activerecord

At the top of Ruby program we need to require the Active Record gem previously installed. Interacting with your database will be a pleasure now!

1
require 'activerecord'

Establishing a connection to database

1
2
3
4
5
6
7
ActiveRecord::Base.establish_connection {
  :adapter => "mysql",
  :host => "localhost",
  :username => "root",
  :password => "password",
  :database => "dbname"
}

Interacting to MySQL Using Ruby

| Comments

This tutorials guides you to go straight into interacting with MySQL database using Ruby alone. No Rails. It is assumed that MySQL has been installed in your computer.

Now let’s get started:

First of all, lets begin with installing MySQL libraries via RubyGems

1
sudo gem install mysql

There is documentation for the MySQL library online . So you can follow that.

Now let’s fire irb.

1
2
require 'mysql'
db = Mysql.new('localhost', 'user', 'password', 'database')