Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Wednesday, October 24, 2012

gzip compression/decompression in Ruby

Recently I did some investigation compression and decompression in gzip format using Ruby. Here I posted my ruby code:

Sunday, October 21, 2012

How to render JSON in rhodes mobile framework

Currently I am using Rhodes framework to build mobile applications. Recently I just figured out how to request an Ajax call from view, and controller how to render a JSON object and return it to the view.

Sample code
Ajax call to request json object using jQuery:

code of rending json in controller


I found this solution in the Rhomobile launchpad forum, it works perfectly.
This exactly looks like render json behaviour in Ruby on Rails.

Sunday, September 23, 2012

Using alias_method to implement AOP in Ruby

This week I just realize the power of ruby alias_method, you use alias_method to intercept the existing method, and modify its behaviour,  this is exactly what AOP does the jobs.

The following code example tell you how to use the alias_method, to add a logging logic to the exit() method.

This reminds me the javaasist framework, I used before, which is a powerful tool to do bytecode manipulation, it can intercept the method by renaming the existing method to another method,  and modifying the existing method, which is the exactly similar strategy.
the code example:
Now when you compare two solution, you will tell how easy to do it in Ruby!

Resource
1. alias_method API document
2. IBM Javaassist tutorial 
3. Spring AOP document

Sunday, January 17, 2010

Ruby and AOP

Right now I am learning Ruby, I focus on Dave Thomas' famous pickaxe Ruby book, last week I just finished the chapter 24: "Metaprogramming".Now I learn the power of Ruby Metaprogramming, right now it is still quite difficult for me, I am sure I will read this chapter over and over in future.

Today I want to discuss my impression of AOP concept in Ruby.
I learned AOP from java, in java you have to use AspectJ or Dynamic Proxy implement AOP. But comapre to Java, I realize Ruby support AOP in the lanuage level:
  • Classes are open: you add new method or attribute for any existing class, or replace the method with new one.
  • Module and Mixin: you can easily add new functionalities by Mixin the module.
  • MetaProgramming: you can dynamically define a method on the fly, using hook method as call back, easily implement before, after and around advice
I feel by using the above technologies, it is quite easy using AOP concept in Ruby, for example you can easily implement a Logging Aspect in a Module, then get it mixin in your application code.That is separating of concern, which is the essence of AOP.
So I will say AOP is built in support in Ruby language level.

Today I found a blog called "Does Ruby need AOP?", the author argue that Ruby metaprogramming is not AOP,because he think AOP is all about semantics, while Ruby metaprogramming is too low level. I feel this argument is too academic, from the pragmatic perspective, I think as long as Ruby can do the job of separation of concern, then it is AOP, then who care about the semantics?

Here are some useful links:
  1. another blog about ruby and APO at 4 line of code
  2. slide: AOP of Ruby