Thursday, 21 August 2014

Something stackoverflow geeks dont know.

 I have used stack overflow for afew months.This site was started to act as a platform for programming Q and A. The site is doing great save for some pot holes and if you bump into one it really sucks...

To those that down vote peoples questions and answers some times with funny reasons first know that there is always a beginning for every thing especially to noobs. I would rather you first offer a solution before you jot all that criticism you comment on people's questions and answers. No wonder some claim the person asking does not have  clear knowledge of the subject.Remember no one can ask a question unless they dont have the answer.Questions are challenges and if instead of offering a solution you want to show how mean you can be shame on you...That is uncultured hubris and for me thats a luxury I will never afford.

" Real geeks just solve problems they do not just wave their hands and whine about the problem they are trying to solve" 





Tuesday, 19 August 2014

Design Patterns in Ruby.

The life of a software developer involves dealing with complex problems during their operations. The first time  a developer confronts a problem he/she improvises a solution but when the same problem occurs often then there is need to come up with a stardard operating procedure to cover it. Thats what design patterns are for.

From wikipedia's definition ; 

In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into source or machine code.

In this post I will give a brief background on a pattern and show its implementation in ruby.I hope you will be able to judge when to use the patterns during your operations.

I will keep updating this post each day with a new pattern(GoF patterns) because posting all of them at once needs alot of time so I will do it in bits.

Singleton Pattern

This pattern is useful when you want to make sure that a class has only one instance and you need to have a global access to it.The singleton module in ruby library can be used to implement this pattern. 

require 'singleton'

class SingletonClass
  include Singleton

  def initialize
    @file = File.open("log.txt", "a")
  end

  def show(content)
    @file.puts(content)
  end
end



Template Method Pattern

The template pattern is useful when you have a method whose algorithm varies as you will see in the example.The idea behind a template pattern is that you have a base class which contains abstract methods(also called template methods) that  the concrete subclasses  will  implement.You can then pick the variation you want by selecting one of the concrete subclasses.


class Good

    def basic_tax(cost)
        raise 'Called abstract method: basic_tax'
    end   
end

class TaxedGood < Good
    TaxRate = (10.00/100)
   
   
    def basic_tax(cost)

        tax = TaxRate * cost
        return tax.round(2)

    end

end

class ExemptedGood < Good
    def basic_tax(cost)
        tax = 0.0
        return tax
    end

end


You can then pick a variation by picking one of the concrete subclasses like this

tax = TaxedGood.new
tax.basic_tax(400)


or

tax = ExemptedGood.new
tax.basic_tax(400)



Strategy Pattern

The Strategy pattern is similar to the template pattern only that it works on the principle of delegation and provides for run time flexibility since we make our decision by selecting a strategy at runtime. The idea behind the strategy pattern is to define objects (also refered to as strategies by the GoF) with similar methods in our example basic_tax; and have them support exactly the same interface(called a strategy by the GoF).Then we define a class(refered to as the context by the GoF) that uses the strategy to treat the strategies like interchangeable parts(since from the outside they look the same as they support the same interface).we make our decision by selecting one of the objects(strategies) at runtime.

class Good

    def basic_tax(cost)
        raise 'Called abstract method: basic_tax'
    end

   
end

class TaxedGood < Good
    TaxRate = (10.00/100)
    def basic_tax(cost)

        tax = TaxRate * cost
        return tax.round(2)
    end

end

class ExemptedGood < Good
    def basic_tax(cost)
        tax = 0.0
        return tax
    end

end

class Context
    attr_accessor :good
    def initialize (good)
        @good = good
    end

    def basic_tax(cost)
        puts "#{good.basic_tax(cost)}"
    end
end


You can then pick a strategy at runtime like this

tax = Context.new(TaxedGood.new)
tax.basic_tax(500)


or

tax = Context.new(ExemptedGood.new)
tax.basic_tax(500)


Interator Pattern

This is a technique that allows an aggregate object to provide the outside world with a way to access its
collection of sub-objects sequentially without exposing its underlying represenation.

In ruby the iterator pattern is implemented in two flavours;
- External iterators.
- Internal iterators.

Internal Iterator

def for_each_element(array)
    i = 0
    while i < array.length
    yield(array[i])
    i += 1
    end
end

a = [10, 20, 30]
for_each_element(a) {|element| puts("The element is #{element}")}


External Iterator

array = ["joan","sheena","gigi"]

i = ArrayIterator.new(array)

while i.has_next?

    puts "item : #{i.next_item}"
   
end

Ugandan Telecoms: Were you ready for this Simcard Re-registration Ocassion!!!!

There was a recent directive from Uganda communications commission calling upon Ugandans to re-register their simcards this time using thei...