Archive for April, 2009

Introduction For Pattern

Posted in Design Pattern with Java, C++ and Ruby on April 6, 2009 by Asanga Bandara Wijekoon

I hope to share my knowledge on design patterns with you and I will post series of posts on this topic. First I am going to discuss what is design pattern and why people use this and then list what are the design patterns I am going to cover here. I will explain each pattern using Java,c++ and Ruby example codes. Usually pattern will not depend on a language user of it should have to have the ability to use it with any programming language.

A design pattern is a solution well tested by thousands of programmers and it gives solution for a problem in a context (problem domain). When we are using pattern for solve a problem always should consider following fact.

“Don’t consider pattern first and push it to solve the problem by forcing”

Good designers are not try to push patterns for the solution by force manner and they apply pattern in natural way. When they design solutions them fit to some patterns and then they used them. They strongly recommend that don’t make more complex the solution by pushing patterns to it in an unnecessary manner. Anyway for simple system also, its good to use a pattern if it drive for changes in future because by using pattern we can easily change the code. Other than that I need to mention here about the “forces” of a design, which is a buzzword in system designing and it consists of problem and the constraints in the context. Finally in this post I list patterns I’m going to address in my future posts in below,

  1. Observer pattern
  2. Decorator pattern
  3. Factory pattern
  4. Singleton pattern
  5. Command pattern
  6. Adapter pattern
  7. Facade pattern
  8. Template Method pattern
  9. Iterator pattern
  10. Composite pattern
  11. State pattern
  12. Strategy pattern
  13. Proxy pattern
  14. Compound pattern

Ruby Time object issues

Posted in Ruby On Rails on April 6, 2009 by Asanga Bandara Wijekoon

I faced this problem when I try to deduct database stored time objects. My self and I think most of developers use time object to store time. But time objedct create and store with an aditional information like year, month and time, etc.

So when we try to decduct two time objects it just not give us only the time difference as what we belive. It calculate the difference of both time object not only considering the time but also with considering year, month and day difference.

So be aware about that when u are calculating the time difference and I mention some methods to overcome that issue in below.

(time_object_2) – (time_object_1)%86400

this will give only the time difference. Other than that u can cast all the time object to a current time object year, month and day by using

today=Time.now #today time object

time_object # which is the time object we neet to cast for certain year month and day

cast_time_object=Time.mktime(today.year,today.month,today.day, time_object.hour, time_object.min, time_object.sec,0)

code. Now we can deduct time objects and we can calculate correct time different.


How to access rails inner hash with in a hash

Posted in Ruby On Rails on April 6, 2009 by Asanga Bandara Wijekoon

Most of the time u are posting u’r post with hash with include another hash. As an example I copy a post parameters I have posted.

Parameters: {“commit”=>”Create”, “date”=>{“minute”=>”30″, “hour”=>”19″}}

I copped this from mongrel  server console.  So how access the :minute value?

Ruby give us a way for accessing inner hash with another hash. So we can access this using following method

params [:date][:minute]

Rails automatically assign post parameter to the “params” hash. Here params is the first hash and we access the date hash with in it. Then we access it’s minute key and get the value.

Rails Ajax and tr issues

Posted in Ruby On Rails on April 6, 2009 by Asanga Bandara Wijekoon

I think this post may help for rails new users. First I will mention how I faced the problem.

I used pure html and prototype functions with in a <tr> tag.

code is

<img src=”/images/delete_img.png” onclick=”new Ajax.Updater(’show_time_table’,'/showtimes/<%=showtime.id%>/’, {method:’delete’})”/>
I used this with in a <tr> tag and I don’t mention about the <td>. Here ajax call not execute until I put this code inside a <td> tag.