f.error_messages in Rails 3.0

since I’m learning Ruby on Rails currently, and while running server on production mode with “rails server -e production”, i noticed that f.error_messages is deprecated with rails 3 and server pops up this error

ActionView::Template::Error (undefined method `error_messages’ for #<ActionView::Helpers::FormBuilder:0x905a7b8>):
1: <%= form_for (@project) do |f| %>
2: <%= f.error_messages %>
3: <p>
4: <%= f.label :name %>
5: <%= f.text_field :name %>

I spent a fair amount of time googling around this to find a solution and i ended up with this solution

<%= form_for (@project) do |f| %>
	 <% @project.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
	<p>
		<%= f.label :name %>
		<%= f.text_field :name %>
	</p>
	<%= f.submit %>
<% end %>

notice we used @project.errors.full_messages.each and looping through errors, rather than old method f.error_messages, though don’t forget to add

gem 'dynamic_form'

to Gemfile, hope this helps anyone facing this problem.

Happy Coding!

Tutorial: The Power of Plurals in Android

Plurals in Android:

Plurals are a type of resource, we use it very often to deal with plurality of Strings, what does plurality of Strings means ?

well suppose you are counting books and you want to print out the result, you will have to write at least 2 Strings like:

<resources>
<string name="zero">there is no books !</string>
<string name="one">there is one book</string>
<string name="moreThanOne">there are %d books</string>
</resources>

and then you will add some logic to your code

       if (booksCount ==0)
{
result= getString(R.string.zero);
}
else if (booksCount ==1)
{
result= getString(R.string.one);
}
else if (booksCount >1)
{
result= getString(R.string.moreThanOne, booksCount);
}
textView.setText(result);

and that is not good to keep languages logic inside your code, imagine you have many internationalized text and every languages have its formatting rules !
here comes plurals to solve this problem:

we are going to use “Plurals” tag rather than “String” tag

<resources>
<plurals name="numberOfBooks">
<item quantity="one">%d book!</item>
<item quantity= "other">%d books!</item>
</plurals>
</resources>

note that each language have limited quantity values based on grammar of each language, for English there are only two values of quantity “one” and “other”,  “zero”  is not allowed in english

we are going to use getQuantityString method

int booksCount= 20;
String result = getResources().getQuantityString(R.plurals.numberOfBooks, booksCount,booksCount);
textView.setText(result);

result should be “20 books!”

another example shows the power of plurals with internationalized text “Arabic” :

<resources>
<plurals name="numberOfBooks">
<item quantity="zero">لا يوجد كتب !</item>
<item quantity="one">يوجد كتاب واحد !</item>
<item quantity="two">يوجد كتابين !</item>
<item quantity="few">يوجد %d كتب</item>
<item quantity="many">يوجد %d كتاباً!</item>
<item quantity= "other">يوجد %d كتاب!</item>
</plurals>
</resources>

you notice here there are many values in arabic such as ” zero”, “two”,”few”,”many” this is because of arabic grammar, it follows these rules

zero → n is 0;
one → n is 1;
two → n is 2;
few → n mod 100 in 3..10;
many → n mod 100 in 11..99;
other → everything else

and for full list of Languages Plurals Rules click here

for Android Plurals Documentation click here

Happy Coding!

Top 5 Things i learned from my Graduation Project

Life is a set of experiences that you learn day by day,  in this post i will share top 5 things i learned from my Graduation Project:

1) Choose your Graduation Project team very well:  this is the most important step in my humble opinion, never choose your friends with no skills because they are just  your friends, this will affect the team badly!, it is likely you are pushing a car, would it be easy if only 3 of 5 person are pushing it rather than 5 persons ?

2) I learned how to read scientific papers:  before starting my graduation project each time i open a scientific paper,  the first thing that comes up to my mind (WTH is this ?!) , i learned that understanding scientific paper very well, may take months, i learned that each paper doesn’t explain everything, just headlines and to read references for more details.

3)Long-Term project:  through our college projects we didn’t spend a long time on any project, most of projects were small ones that takes max 3 weeks, so it was a new experience to work on a project for  a whole academic year, in the beginning you feel very ambitious, in the middle it is likely you will say ” i wish to finish this project today before tomorrow”, and when the project is done,  you will be amazed by the results and amount of work you have done.

4) Apply the Damn Theory : during my studies in college, i have always loved practical courses, always hated theoretical stuff!, i think the top reason why i hated theoretical stuff was that we don’t apply most of  it !, i didn’t see most of it in action !, in my graduation project i had to read so many scientific papers with tons of equation and apply & implement them in our project.

5) Time to Apply for a Master’s : before starting my graduation project, i never thought about doing a Master’s in Computer Science, i learned a lot and got more interested in another fields and now i have more passion to learn more about these fields, if you are willing to do your Master’s in another university or with a different professor, your Graduation project will be a great plus in your CV.

Now, how about you?!, what did you learn from your Graduation project?

Reservoir Sampling – Java

I’m currently preparing for the first semester exams and while browsing over the Internet i found some one posted his interview with google and there was a question about Reservoir Sampling, i got interested and started googling about how this algorithm works and i ended up writing this post, i explained this algorithm as simple as i can, enjoy the science 😉

what is Reservoir Sampling ?

Reservoir sampling is a family of randomized algorithms for randomly choosing k samples from a list S containing n items, where n is either a very large or unknown number.  (From wikipedia)

Suppose you have unknown number of queries (google search queries last month for ex.) and you want to select random sample  from this unknown number of queries

what is the problem with unknown size of queries?

you can’t use random function directly here, there is no limit here , you may refer to a wrong index !,  fine why can’t we loop through the queries and get the numer of queries ? , simply this will increase complexity of the algorithm with another O(n).

so, how to choose random sample from this unknown number of queries using only one pass O(n) , here comes the Reservoir Sampling Algorithm:

for simplicity i will use a text file inclues a long article like this one Open Source i copied the article and saved it in a text file name “data.txt”, suppose we don’t know how many lines in this article . Continue reading

HOW TO: Make a System Tray Icon in Java in 3 Steps

to Create System Tray we need 3 things:

1)PopupMenu

2) Menu Items

3)  TrayIcon

Step # 1 :-

so, i will start with PopupMenu

PopupMenu popMenu= new PopupMenu(); 


Step # 2 :-

now we go to step #2 creating menu items, every menu have menu items that you click on it


MenuItem item1 = new MenuItem("Exit");  //here i created menu item named item1 and the text will appear in this menu item is "Exit"

Continue reading

My Notes on OCPJP(SCJP) – Chapter 1

SA,

I’m Currently Preparing for OCPJP (SCJP) Exam and i thought it might be useful to post my Notes on every chapter of  Kathy Sierra and Bert Bates book  “SCJP Sun Certified Programmer for Java 6 Exam 310-065” , you can not study from these notes, you must read every chapter from the book, these notes for everyone who wants to make a quick review on every chapter, i will post 10 posts (1 post/chapter),and here is the first Chapter of the Series,

Chapter 1: Declarations and Access Control

Instance Variables: Variables defined inside a class that each Object have a copy of it.

There is Three Types of Identifiers :

1) Legal Identifiers:

a) Starts with $,_,Letters but can’t start with a Number, after the first letter we   can add Number to Identifier.

b)They are Case-Sensitive.

c) No limit to Number of Characters an identifier can Contain.

2) Sun Code Conventions:

a) with Classes /Interfaces: class Name Must Start with a Capital Letter.

b) with Functions/Methods:  method name must start with a small letter.

c) with variables : same as methods must start with a small letter.

d) with Constants: all Constant Characters Must be CAPITAL.

3) Java Beans Standards :

a) Functions that doesn’t return boolean called Getters and must start with get for example : getSize()

b) Functions that return Boolean may start with is/get but “is” is perfered isValid()

c) Function that set values must start with set called Setters setSize()

d) Setters Must be Void and Accept Parameters. Continue reading

Android System Architecture

I recently started learning developing applications for android OS , i liked it much however developing applications for android is kinda different from what we used to , you must be aware of Android System Architecture, so you can develop powerful applications,  let’s start with this Diagram that Explains the Levels of Android System Architecture:

There is 4 levels of android architecture , let’s start with Level #1:

Level #1: Linux Kernel :

Android uses Linux as a hardware abstraction layer and use the powerful of Linux kernel and the wide range of hardware drivers it supports, android uses Linux also for memory management , Networking , managing processes, however you programs will not make Linux calls directly , you always use the Dalvik(Android Virtual Machine).

Level #2 :  Native Libraries & Android Runtime:

Native Libraries :

these libraries are written in C/C++ already compiled and preinstalled by vendor for particular hardware abstraction, there is alot of important native libraries such as :

Surface Manager: Android uses a compositing window manager similar to Vista or Compiz, but it’s much simpler. Instead of drawingdirectly to the screen buffer, your drawing commands go into off-screen bitmaps that are then combined with other bitmaps to form the display the user sees. This lets the system create all sorts of interesting effects such as see-through windows and fancy
transitions. Continue reading

First Post

Al Salamu Alykom Wa RahmatULLAH Wa Barakato (peace be upon you)

This is my first post on this blog ,  truly this is my first time on wordpress , as i have always used Blogger , i never thought of making personal blog yet , but the idea came up to my mind , why not share opinions and knowledge with other people ? , i will try to add new posts from time to time :).