Friday, January 20, 2012

Randomly generate string


Hello Guys,
        If you want to generate random string with specific some constraint like at least single upper case, lower case and digit. or many more.


Here i have found gem called 'passgen'. which provides many possible changes.
gem install passgen


On console:
require 'rubygems'
require 'passgen'
Passgen::generate(:lowercase => true, :uppercase => true, :digits => true, :length => 10)


For more information follow the url - https://github.com/cryptice/Passgen

Wednesday, January 4, 2012

Undefined method Object.keys


Hello Guys,
        Object.keys is depreciated and it does not support in some of the browsers like IE, FF 3.x. So we have to add the javascript object.keys method to overcome this problem.

Object.keys = function(obj) {
   if (typeof obj != "object" && typeof obj != "function" || obj == null) {
     throw TypeError("Object.keys called on non-object");
   } 
   var keys = [];
   for (var p in obj) obj.hasOwnProperty(p) && keys.push(p);
   return keys;
}

Now see we resolve the javascript error 'Undefined method Object.keys'.

Duck Typing in Ruby

Hello Rubies,
       Ruby is an object oriented programing language. we don't declare the data type of variables or methods - everything is treated as object. Ruby supports feature called 'duck typing'. Duck typing more concern about what's the behavior of method and what methods can be called on it.

For eg.
We create the simple method to do the multiplication of two arguments. As in ruby we didn't define the data type. So it return the output of method in requested method format.

def calculate(a, b)
  return a * b
end

case a. 
   Pass integer as argument
   calculate(2, 3) => 6
case b.
   Pass string as argument
   calculate('Ruby on ', 'Rails') => Ruby on Rails

So as per argument type it return back the result in requested format. So this is the concept of duck typing. 
If we have to implement same functionality using java than it requires to define methods as per requested type. so when we invoke the method than it first match argument type than return the appropriate result. 

So now you know how cool is ruby than java ;)

Monday, January 2, 2012

Accept argument during .sql file invocation

Hello,
        Some time we have to set the variable of .sql file run time. In that case require to take input and set the variable accordingly.

For eg. There is sql file called 'tablefunc.sql' in which default search_path is set to public but we require it to be dynamic. Now we need to execute the .sql file as

In .sql file
SET search_path = :search_path;

Invoke sql file using system command
config = YAML::load(File.open('config/database.yml'))
pg_schema = 'demo'

system("psql -U #{config[env]['username']} -d #{config[env]['database']} --variable search_path=#{pg_schema} < lib/tablefunc.sql")