wxWidgets is a nice library used to build GUI applications in C++. Here is how i installed(version 2.8) it:

1. Go to Synaptic Package Manager

2. Search libwxgtk2.8-dev package and mark it for installation.

3. Apply changes and it should install all the dependencies.

Testing installation:

1. Fire up a terminal.

2. Type  wx-config –version

3. You should see the version output by the above command.

Compiling and running a test program:

1. Fire up emacs or your favorite editor

2. Type the following program:

#include<wx/string.h>
using namespace std;
int main(){
wxPuts(wxT("A wxWidgets console app"));
return EXIT_SUCCESS;
}

3. Save the program and give it a name(say, wxSimpleConsole.c++)

4. Since i am running gcc, here is how you compile:

g++ `wx-config –cppflags` `wx-config –libs` wxSimpleConsole.c++ -o wxSimpleConsole

5. Run the binary like this:  ./wxSimpleConsole

This is what you get:

A wxWidgets console app

 

My earlier post was really dumb about simulating  the reduce method. Not quite the object oriented way after pointed out by Nikhil. So after searching the Ruby docs for more methods, i came across the ‘send’ method. And it was all i wanted:

class Range
 def reduce(arg)
   self.send arg #This is where the action is! send would evaluate the symbol to a method name
 end
 def +
   res = 0
   each do |item|
    res += item
   end
   res
 end # + ends

 def *
   res = 1
   each do |item|
    res *= item
   end
   res
 end # * ends
end #Range ends

r = Range.new(1,5)
p r.reduce(:*) #=> 120

The method_ missing function in Ruby is an interesting one i must say. It lets me do something cool. This was when i wanted to overload the parentheses. After reading a blog on a smart hack to overload the parentheses,  i thought if i could do it at the object level. Well, this is what i came with so far. Havent tested everything and the object in caps look wierd. Let me have a go at it later.

 
class Object 
  def method_missing(m, *args, &blk)
   puts "in object for call : #{m},  args: #{args}"
   self.class.const_get(m).foobar
  end
end #Object ends
class Klass
  def foobar
   puts "foobar called"
 end
end #Klaas ends
K = Klass.new
K(1,2,3)

The o/p:

>in object for call : K,  args: 123
>foobar called

While looking at the reduce method, i thought how i could simulate it. So extended the Range class and overrode the reduce method. However, i dont know if this is the really the correct way to do it. I mean there could be a more better way to implement this.

class Range
  def reduce(sym)
    res = 0
    case sym
    when :*
      res = 1
      each do |item|
        res = res * item
      end
    when :+
      each do |item|
        res = res + item
      end
    else
      res = 0
    end
    
    res
  end
end
puts (1..5).reduce(:+)

The output is: 15

This is my first version of the inject method in Ruby:

class Array
 def painful_injection(n)
   sum = 0
   each do |item|
     sum = yield n, item #pass the arguments to the block
     n = sum #reassign the first arg
   end
   sum
 end
end
puts [1, 2, 3, 4].painful_injection(0) {|sum, nxt_item| sum + nxt_item} #=> 10

Still have to make it such that the argument to the painful_injection method is not needed.

I came across a ruby assignment to simulate grouping of items in a collection based on the given condition. This feature is there in 1.9 version(dont know exactly which). So, i wrote this to simulate the groupby:

class Array
 def group_by
   ar = Hash.new{|key, value| key[value] = []}
   each do |item|
     ar[yield item] << item   
   end #loop ends
   return ar
 end #groub_by ends
end #Array ends

p [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].group_by{|item| item % 2}

This produces:

    {0=>[2, 4, 6, 8, 10], 1=>[1, 3, 5, 7, 9]}

Or,  i could  just avoid that long Hash definition by using the T-square operator do tell ruby that ar[yield] is an array:

class Array
 def group_by
   ar = Hash.new
   each do |item|
     (ar[yield item]||=[]) << item  
   end #loop ends
   return ar
 end #groub_by ends
end #Array ends

The yield method is a really interesting utility to implement your own iterator. From the PickAxe book, the first chapter tells how powerful this utility is and shows a pseudo-code to accomplish the ‘each’ iterator. Here is how i implemented it:

class Array
   def myeach
         for item in self
              yield item #item is passed to the code block associated with myeach
         end
   end
end

%w{this is an array}.myeach{|item| puts item} #code block of myeach

OUTPUT:

this
is
an
array

Scanning through the Ruby 1.8.7 API, I came across  this simplest code to find factorial of any number.  Oh! This language is simply magical.

(1..5).reduce(:*)
=>120

And i found that I can also do this:

(1..gets.to_i).reduce(:*)
100
=> 93326215443944152681699238856266700490715968264381621468592963895217599993229
91560894146397615651828625369792082722375825118521091686400000000000000000000000
0

I scan the range value using gets and convert it obviously to an integer.  It works like charm :)

Operator overloading has never been so easy in any of the languages i have coded in so far. Ruby continues to surprise me every single time i do something in it. Cheers to the simplicity! I was reading Beginning Ruby: From Novice to Professional and came across a simple code explaining class methods. I experimented a bit right from renaming the method from a string to the ‘-’(minus) character:
   class Person
 def initialize(age)
   @age = age
 end
 def age
  @age
 end
 def -(person2)
  self.age - person2.age
 end
end #Person ends
abhi = Person.new(24)
kirti = Person.new(22)
abhi.-(kirti)   #this gives output: 2
abhi -(kirti)   #this gives output: 2

This is the best thing:

   abhi - kirti    #this gives output: 2

I have come back to Ruby and thought about how i can implement the Singleton pattern here. Well, after much reading and lots of help(thanks linoge and heftig) from the #ruby channel on freenode, i came up with this code and the resulting output:

 class Singleton
   attr_accessor :singleton_obj #attribute
   private_class_method :new #making the new method private

   def initialize #ctor
   end
   public

    def Singleton.getInstance #clss method
         if @singleton_obj == nil #chk if instance is null or not
             puts "Creating new obj and returning it"                                                                                                                                                                                                                               @singleton_obj = new #this method inherited from the Object 	
             return @singleton_ob
         else
              puts "Returning existing object"
              return @singleton_obj
         end
     end #getInstance ends

     def display #instance method   
          puts "called"  
     end
 end #Singleton ends



  singleton_obj = Singleton.getInstance #get a new instance;   
  singleton_obj.display

  singleton_obj2 = Singleton.getInstance #get a new instance;
  singleton_obj2.display

  singleton_obj3 = Singleton.new #create a new instance;
  singleton_obj3.display
   
   ##output:
   kenshin@kenshin-desktop:~/st0p/programming/Ruby$ ruby Singleton.rb
   Creating new obj and returning it
   called
   Returning existing object
   called
   Singleton.rb:32: private method `new' called for Singleton:Class (NoMethodError)

    Follow

    Get every new post delivered to your Inbox.