Grails and Arabic localization

A little problem that i faced with grails , was setting the locale dynamically, i followed this nice article , and here is how i could manage it :

  1. I made a new file called messages_ar.properties that contains the arabic localization under the i18n folder.
  2. I made a simple beforeinterceptor:
    def beforeInterceptor = {
            def key = "org.springframework.web.servlet.DispatcherServlet.LOCALE_RESOLVER"
            def localeResolver = request.getAttribute(key)
            localeResolver.setLocale(request, response, new Locale("ar","JO"))
        }

Of course you can use grails/Filters to save yourself repeating the same code in every controller.

However what i want to mention is the locale object, which in case of Arabic localization, was seeking the country argument ( “JO” in our case) :

new Locale("ar","JO")

While for other localizations like German one it doesn’t seek this second argument, so this snippet of code will work for them :)

new Locale("de")

Also you can change this country argument value to match this rule :

The country argument is a valid ISO Country Code. These codes are the upper-case, two-letter codes as defined by ISO-3166. You can find a full list of these codes at a number of sites, such as:

http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html

I hope that was helpful :) , enjoy!

Posted in Grails | Tagged , , | 2 Comments

Ruby dynamic method calling

I’m pretty sure that you have heard lots about ruby, specially as being a dynamic language, you can create methods on the fly, add instance variables, define constants and invoke existing methods dynamically , and that’s what this post is all about :

As you know in ruby you can call a public instance method directly ,ex :

s= "hi man"
p s.length #=> 6
p s.include? "hi" #=> true

One way to invoke a method dynamically in ruby is to send a message to the object :

p s.send(:length) #=> 6
p s.send(:include?,"hi") #=> true

A second way is instantiate a method object and then call it:

method_object = s.method(:length) 
p method_object.call #=> 6
method_object = s.method(:include?)
p method_object.call('hi')  #=> true

And the third way is to use the eval method:

eval "s.length" #=> 6
eval "s.include? 'hi'" #=>true

Well, when to use what?

look at this script, it will be used to benchmark the 3 ways of calling :

require "benchmark" 
test = "hi man" 
m = test.method(:length) 
n = 100000 
Benchmark.bmbm {|x| 
  x.report("call") { n.times { m.call } } 
  x.report("send") { n.times { test.send(:length) } } 
  x.report("eval") { n.times { eval "test.length" } } 
} 
#######################################
#####   The results
#######################################
#Rehearsal ----------------------------------------
#call   0.050000   0.020000   0.070000 (  0.077915)
#send   0.080000   0.000000   0.080000 (  0.086071)
#eval   0.360000   0.040000   0.400000 (  0.405647)
#------------------------------- total: 0.550000sec
 
#          user     system      total        real
#call   0.050000   0.020000   0.070000 (  0.072041)
#send   0.070000   0.000000   0.070000 (  0.077674)
#eval   0.370000   0.020000   0.390000 (  0.399442)

Well as you can see, instantiating a method object is the fastest dynamic way in calling a method, also notice how slow using eval is.

Also when sending a message to an object , or when instantiating a method object , u can call private methods of that object :

class Foo
  private  
  def hi 
    puts "hi man" 
  end 
end
 
# Normal method calling
f = Foo.new  #=> <Foo:0x10a0d51>
f.hi  #=>NoMethodError: private method `hi' called for #<Foo:0x10a0d51> 
 
# Sending a message
f.send :hi #  hi man
 
# Instantiating a method object
f.method(:hi).call  # hi man
 
# Using eval
eval "f.hi"  #=>NoMethodError: private method `hi' called for #<Foo:0x10a0d51> 
 
# Using instance_eval
f.instance_eval {hi}  # hi man
Posted in metaprogramming, Ruby | Tagged , , | 18 Comments

Ruby introspection

Hi, this is my first blog post!, i’m already done of reading this artilce on groovy’s lang introspection , and i wanted to submit the equivalent one for ruby, so all you need now is to fire your irb and follow me :) :

# Whats is your class?
 
a = 5
b = "Hello"
 
# Whats is your class?
p "Class of a : #{a.class} ,class of b : #{b.class}" #=>"Class of a : Fixnum ,class of b : String"
 
# Whats is your super class?
p "Super class of a : #{a.class.superclass} ,super class of b : #{b.class.superclass}" #=>"Super class of a : Integer ,super class of b : Object"
 
# Is a.class = Fixnum ?
a.instance_of? Fixnum #=> true
 
# Is a of type Integer (does it have Integer in it's class hierarchy)?
a.is_a? Integer #=> true
 
# Or this way:
a.kind_of? Integer #=> true
 
# Introspection, know all the details about classes :
# List all ancestors(modules and classes) of a class
String.ancestors.each{|a| p a}
 
# List all modules included in a class
String.included_modules.each{|m| p m} 
 
# Check class hierarchy
String < Comparable #=> true
String < Integer #=> nil  , strings r not integers
Object < String #=> false , Not all objects are strings
 
# List ancestors of class type
String.ancestors.select{|a| a.class==Class}.each{|c| p c}
 
# List all methods available to an object
b.methods.each{|m| p m}
 
# Get public instance methods
String.public_instance_methods.each{|m| p m}
 
# Get protected instance methods
String.protected_instance_methods.each{|m| p m}
 
# Get private instance methods
String.private_instance_methods.each{|m| p m}
 
# Get class singleton methods
String.singleton_methods{|m| puts m}
 
# Get the instance variables of an object
d = Date.new
d.instance_variables.each{|i| p i}
 
# Get public instance methods
d.public_methods.each{|m| puts m}
 
# Get protected instance methods
d.protected_methods.each{|m| puts m}
 
# Get private instance methods
d.private_methods.each{|m| puts m}
 
# Get instance singleton methods
d.singleton_methods.each{|m| puts m}

As for the Dynamic method calling introduced in that article , check this post ;)

*Update : Check the second part article of ruby introspection, for more info on this topic.

Posted in metaprogramming, Ruby | Tagged , , | 1 Comment