Posted by Sergey Enin
29 August 2011 at 22:13
We are all use blocks in our ruby programs, but what if we`d like to store somewhere or pass anonymous code to some method.
It is neccessary to do for example in Strategy pattern, where
1) we are defining or load some strategies;
2) we are passing one of strategy to object to call some action;
For that purpose Proc and lambda are very usefull.
Posted in Ruby, Ruby` Short | Tags bind, Binding, closure, lambda, lambdas, proc, procs, ruby, unbound
Posted by Sergey Enin
05 August 2011 at 18:12
Sometimes, it is neccessary to build tree of some class subclasses. For example when plugins in ruby application inherit from some Class and you want to control it. For that purpose you should use ObjectSpace.each_object.
I placed example below, to use it you need:
1) Object.class_eval("extend ClassModule")
2) and now you can simply Numeric.subclasses or Numeric.successors
For example, I runned it on my machine(excluded some Gem, RubyVM and other special classes) to get Tree of all Classes avaible for RubyVM at current time:
or if you prefer images:
you can get it.
ActiveRecord tracking
If you want tracking of creating classes, you can extend ActiveSupport::DescendantsTracker module.
After it you classes will start to watch after the descendants.
Thanks
Thank you.
Posted in Ruby, Ruby` Short | Tags class, module, object, ruby