Procs, Lambdas and closure.

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 ,  | Tags , , , , , , , ,

Class` descendants

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 Petteri Räty for that info.

 

 

Thank you.

Posted in ,  | Tags , , ,

Uninitialized variables

Posted by Sergey Enin 31 July 2011 at 04:37

In comment to one of previous posts wrote how usually ruby developers solve uninitliazed variables problem in conditions:

if (!!@a)

This works, cause:

1) in Ruby, everything evaluated as TRUE, but not FALSE or NIL!

2)  > !nil
 => true
#so, !!nil => false;

3) in Ruby uninitialized instance variables evaluated as nil;

 

I dont believe it is best way(though possible). Consider, that if you are cared programmer and develop with "ruby -w" ruby will write you warning.

 

However, I decided to review uninitialized variables in ruby:

 

@@a - Class variables:

ruby-1.9.2-p136 :004 > @@a
NameError: uninitialized class variable @@a in Object

 

@a - Instance variables:

ruby-1.9.2-p136 :003 > @a
 => nil

(if -w - then cause warning)


$a - Global variables:

ruby-1.9.2-p136 :011 > $a
 => nil

(if -w - then cause warning)

 

a - Local variables:

ruby-1.9.2-p136 :001 > a
NameError: undefined local variable or method `a' for main:Object

but

if we will do

ruby-1.9.2-p136 :003 > a = 2 if false
 => nil
ruby-1.9.2-p136 :004 > a
 => nil


A - Constants:

ruby-1.9.2-p136 :002 > A
NameError: uninitialized constant Object::A

 

Thank you.

Links:

Posted in ,  | Tags , , ,

Redmine Plugin Development - Part2: Journal, Customization of Redmine Menu and Core

Posted by Sergey Enin 20 April 2011 at 23:36

Read previous part: "Redmine Plugin Development - Part1: Generation, Enumerations, Hooks"

 

In first part of the guide I reviewed Posibilities to add new functionality to Redmine, but what you if you would need/like to modify exists Redmine functionality.

As I wrote in previous part, I will not covert details described in at Redmine official plugin guide.

 

Posted in , ,  | Tags , , , , , ,

Redmine Plugin Development - Part1: Generation, Enumerations, Hooks

Posted by Sergey Enin 09 April 2011 at 02:40

Currently redmine represents good and powerfull project management system.

It`s free, powerfull and provide a lot customization and extensable features. Third-party features is rational to implement through plugins, than change the core code, - it is avoid problems when others in your team also would like to customize redmine or when you would like update your instance in future.

Especially great that redmine shows how ruby metaprogramming phylosophy transforms into great solution.

I created for myself little manual, where I described all most interested or obscure questions I faced during work with redmine, separated into 2 parts:

1) Generation, Enumerations, Hooks.

2) Customization.

Below is Part 1.

Please, consider, that I will not cover points that good is on Plugin Page.

 

Posted in , ,  | Tags , , , , , , ,