Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

ZuNien Lin

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Acts as Java Class Variable

// Ruby's Class Variable is soooooooooooooooo confusing
// http://www.oreillynet.com/ruby/blog/2007/01/nubygems_dont_use_class_variab_1.html
// However, if we use

   1  
   2  module JCV
   3    def self.included(the_class)
   4      class << the_class
   5        def acts_like_java_class_variable( *arg)
   6  
   7          the_class = self
   8          singleton_class = class << self; self; end
   9          arg.each do |var|
  10            singleton_class.send :define_method, "#{var}",
  11                                 & lambda{ the_class.send "__real_#{var}"}
  12            singleton_class.send :define_method, "#{var}=",
  13                                 & lambda{|c| the_class.send "__real_#{var}=",c}
  14            singleton_class.send :define_method, "__real_#{var}",
  15                                 & lambda{ instance_variable_get "@#{var}"}
  16            singleton_class.send :define_method, "__real_#{var}=",
  17                                 & lambda{|c| instance_variable_set "@#{var}", c}
  18          end
  19        end
  20      end
  21    end
  22  end
  23  
  24  class A
  25    include JCV
  26    acts_like_java_class_variable :count
  27  
  28    @count = 0
  29    def initialize
  30      A.count +=1
  31    end
  32  end
  33  
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS