Ruby refactoring fun

I my last post I’ve said that I don’t care if code for method_missing implementation is ugly. Well I’ve lied.
This is starting point

def method_missing(method, *params)
     method = method.to_s
     type = :get
     if (method.index('store_').nil? == false) #if you have name conflict use store_ prefix,
                                                             #or rename hash key or class method
         method.gsub!('store_','')
     elsif (method.index('exists_').nil? == false)
         method = method.gsub!('exsists_','')
         type = :check_if_exists
     elsif (method.index('set_').nil? == false && params.count == 1)
         method = method.gsub!('set_','')
         type = :set
     end
     method = method.to_sym
     if type == :get
         return @store[method] if @store.keys.collect(&:to_sym).include?(method)
     elsif type == :check_if_exists
        return @store.keys.collect(&:to_sym).include?(method)
     else
        return @store[method] = params[0]
     end
     return nil  # return nil if key does not exist
  end

If you want to see what kid of result did I get read on (more…)

Using Ruby Metaprogramming to Increase READABILITY of Code

Let’s assume you have a class that has some sort of hash where it stores it’s data. We want to calculate amount that we need to pay, but only if number of items that we want to sell is entered. We’ll also assume that price is already present so we don’t need to check for that. It’s easy:

@store[:total] = @store[:price] * @store[:amount] if @store.has_key?(:amount)

A bit more readable would be:

set_total(price * amount) if amount_exists

(more…)

Tweak CalendarDateSelect plugin to work with European datetime format

Calendar date select in action

If you want to use calendar_date_select plug-in for date time picker, and you would like to use dd.mm.yyyy format read on!

I wrote this post after many wrong turns and many tutorials that just didn’t work. This informations applies for RAILS 2.3.2, and Calendar Date Select 1.15. I you are anything like me you want proof that my tutorial is working? (more…)

RAILS auto_complete With Additional FILTER

Auto complete with additional filter

The best tutorial about auto complete control by far was one from Ryan Bates. It worked like a charm, and he made it look so easy. Only problem I had with it was that I wanted another control on the form to filter the auto-complete control.
The tutorial didn’t cover that, so I got stranded in a strange place. The solution was pretty simple, but it took me a while to figure it out. If you are just beginning Rails like I am – this is rigth tutorial for you.

(more…)

Ruby BLOCKS Explained 101

Blocks

Block is basically anonymous method that gets called from another method. If you have some code that executes passed in method like this:

def execute_method_in_block(named_method)
    named_method.call
    named_method.call
end

execute_method_in_block Proc.new {puts "Hello, blocks are cool!"}

You can write it like this using blocks:

def execute_method_in_block
    yield
    yield
end

execute_method_in_block  {puts "Hello, blocks are cool!"}

We’ve lost “Proc.new” and a method name, we are just saying

Whenever you find yield, execute block that is defined within braces.

You might be wondering what’s the fuss all about, (more…)