Thursday, April 19, 2012

Configure mailer with ruby

Hello Rubies,
    As we have to send the mail from rails application for that it requires to set the mailer configuration with rails.

First you have to install the package called 'sendmail'
sudo apt-get install sendmail

Now in environment.rb file of rails application set below code:

ActionMailer::Base.delivery_method = :sendmail

config.action_mailer.smtp_settings = {
  :address => "smtp.gmail.com",
  :port => 587,
  :domain => 'test.com',
  :authentication => :plain,
  :user_name => 'XYZ@gmail.com',
  :password => 'PWD' 
}

Hope this post will help you.

Tuesday, April 17, 2012

Number of days between two dates

Hello Rubies,
    Get the days between two dates using below method of ruby date class.

Lets consider we have date range like
date1 = Date.today.to_date
date2 = 1.year.ago.to_date
no_of_days = date1.mjd - date2.mjd

Hope this simple method of ruby date class will help you out to find days between to dates.

Friday, April 6, 2012

Combination of array element

Hello Guys,
    Want the combination of array elements using ruby? than use the below custom method.

def arr_combine(arr_1, arr_2)
    return arr_1 if arr_2.empty?
    arr_3 = []
    arr_1.each do |e1|
      arr_2.each do |e2|
        arr_3 << [e1, e2].flatten
      end
    end
    return arr_3
  end

eg. we have array like arr = [[1,2], [3,4,5]]
we expect the result as [[1,3],[1,4],[1,5],[2,3],[2,4],[2,5]] than simply invoke the above method by passing each element of given array.
arr_combine(arr[0], arr[1]) gives you required outcome.

Hope this post will help you out.

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")