Wednesday, October 28, 2009

OOPS: Creating object for an Interface is Possible

    Use the below sample code to create an object for an interface. This is called as "anonymous inner class".
What's happening here is that an object gets instantiated that implements the interface.

    public interface MyInterface {
    public void myMethod() ;
    }

    ...

    MyInterface myIntfObj = new MyInterface() {
    public void myMethod() {
    ....
    }
    };

    myIntfObj.myMethod();


Thursday, October 22, 2009

install gems by .gem files

To install gems by .gem files use the below command on command prompt or terminal.

Note: To download .gem files for a specific gem, do a search in rubyforge.org

gem installinstall-dir PATH library_name

then the output will like this
Successfully installed library_name.version
1 gem installed

Hope this will help for someone or me!!! :)

Simple Array Comparison in Ruby

Here is a simple method to compare two arrays using Ruby, few examples:

> [1,2,3] <=> [1,2,3]
=> 0
> [1,2,3] <=> [2,2,3]
=> -1
> [1,2,3] <=> [3,2,3]
=> -1
> [1,2,3] <=> [1,3,3]
=> -1
> [1,2,3] <=> [1,1,3]
=> 1

By use of Terinary Operator (<=>), we can acheive the simple method to compare two arrays of comparison.

Hope this will help for someone or me in future!!!