Previous article, we have discussed about instance_eval function in Ruby. In this article, we are going to go through class_eval function. By understanding the behavior of class_eval function and the difference between class_eval and instance_eval, you will not be confused by the names of these two functions as well as you will know when and where to use each of them

Read the rest of this entry

One of the important topic in Meta Programming in Ruby is instance_eval and class_eval. In this article, we are going to go through the usage of these 2 functions and how can we apply them to write meta programming code in Ruby

Read the rest of this entry

Ruby CSS Parser

January 5th, 2010

In this article, we will go through and explore a CSS parser written in Ruby Language. Once you know and understand about CSS parser, you can go further into other bigger problems like styling a mail using an external stylesheet, or optimizing your CSS file by removing redundant CSS rules or combine multiple CSS rules into shorter rule.

Read the rest of this entry

Using Module in Ruby

August 16th, 2009

When you develop a large application, most of the times, you will find out that you have a lot utility methods to facility other business methods and are reusable throughout the application. At this point you should think of using Module to store those methods and include the module in the classes that you would like to call the methods within. In this article, we are going to discuss the usage and benefits of using Ruby module

Read the rest of this entry

This is the continuous article of previous article http://railstalk.com/2009/8/13/meta-programming-with-ruby. In previous article, we have mentioned about one of the very unique features of Ruby class which is all Ruby classes are open. This does give Ruby programmers a lot of flexibility to extend all the classes - including primitive classes to make the code so expressive like a natural language. Continuing from the last article, in this article, we will mention about the other two very unique features of Ruby which are active method definitions, all method calls have a receiver and Classes are objects

Read the rest of this entry

Have you ever wondered how ActiveRecord can have such nice features like setting relationship just with one line of code - has_many or belongs_to, callback functions - before_create, before_save, ... or validations - validates_presence_of or validates_uniqueness_of... Well, in order to understand those, we are going to have some fun in learning meta programming features with Ruby class, Object and Module. By learning some of the fun and unique meta programming language features in Ruby, we will understand how all the predefined functions in ActiveRecord, ApplicationController can be written so nicely. After that, we will be able to extend Ruby language for our fun and as well as to serve our various purposes when doing programming

Read the rest of this entry

If you use Mephisto as your blog engine, you should have known that Mephisto is not equipped with articles pagination. As a result, you have to figure out how to set up the pagination on your list of articles. There is a myth that it is quite difficult to set up pagination for your blog articles in Mephisto. In fact, the difficulty with Mephisto is that instead of using normal Ruby on Rails, Mephisto is using liquid template to render the articles which is quite new and complex to understand in order to customize up to your expectation.

In this article, we will go through how to set up articles pagination to work with the latest Mephisto blog engine - version 0.8.2

Read the rest of this entry

It seems not maintainable when we have too many gems installed on our local machine gem repositories. Indeed in UNIX machine, our access is granted by user group itself. Let say if one day, your hard disk crashes and you lose all your data. It would be a troublesome to reinstall all the gems one by one. Normally, I only do backup my home folder in this case. So if this incident happens to me, I will need to reinstall all my ruby gems again to get my Ruby on Rails development environment ready to use. It is really too much work to be done.

You can solve the issue by either installing all your gems inside a sub folder you home folder or to freeze your gems inside the application folder.

Read the rest of this entry

Sometimes, in a Rails application, you need to enhance the performance of your Ruby code of certain function. If you have already tried all the methods that you have known, but still cannot make a signifficant improvment, it time for you to try Ruby Inline and convert the slow Ruby code into C within the same ruby code. As you might know, Ruby is quite slow because it is a scripting language, whereas C is lightning fast due to its low level attribute. Converting centain slow Ruby code to inline C code is a smart and neat choice in order to improve the performance of your system. In this article, I am going to go through how we install Ruby Inline and make use of it to speed up the performance of our Ruby code

Read the rest of this entry

Ever heard about Tokyo Cabinet Database? Tokyo Cabinet Database is a simple yet extremely fast database built to cater the simple records each with key and value pair. It is proven to be faster than MySQL, Postgres due to its simplicity. In this article, we are going to go through how to install and use this database in a Ruby on Rails application and see how we can fully utilize the speed of this kind of database.

Read the rest of this entry

If you are familiar with will_paginate gem in Rails, you will know that you can do quite a lot of things with pagination like customizing page links, pages link position, but have you ever tried to have an ajax pagination which means the user can just click on the link and the pagination segment and the content will reload within the page without reloading the whole page. Sometimes, this is extremely useful when you have a search with a lot of categories and you are required to employ in pagination for the search results. Without ajax pagination, you will have to carry all the search filters into the pagination link, which makes the URL ugly and hard to maintain. In this article, I am going to go through how to create an ajax pagination with jQuery

Read the rest of this entry

Ruby you may have missed

August 7th, 2009

In this article, I am going to go through some ruby special and very useful features that you might have overlooked when you use Ruby to code Rails application. These Ruby functions will help you more fall in love with Ruby because of the very nice and easy to remember syntax that Ruby manage to offer

Read the rest of this entry

Have you ever had chance to create a zip file, or read a zip file by Ruby. It might be the case you need your system to zip a list of documents, photos,... to send to the user on the web or through email as attachment. Or you need to archive the old documents, photos to save space on your system when the documents, photos are too old or already expired. Just imagine how helpful that you know how to use Ruby zip if you know your customers cannot stand when they receive an email with more than 5 attachments. In this article we are going to learn how to create and read zip file using Ruby

Read the rest of this entry

In this article, we are going to learn how to use include keyword in ActiveRecord#find function. include is a very powerful weapon to put in use if you know clearly how to. If used in the right place, it can cut down a lot of unnecessary queries that you might not notice as well as can save us from coding raw SQL command to query from database

Read the rest of this entry

In this article, we continue from previous article to learn about Ruby Array inject function. In previous article, we have already mentioned how to do a sum and convert a 2 dimensional array into a hash with inject function. In this article, we will take a look on how we use inject function to convert an array into another array, use inject to convert an array of objects into a hash using one of the attribute inside the object as the hash key

Read the rest of this entry