h1. Context-oriented Programming in Ruby h1. → 'contextr' h2. What TODO h2. Installing Nothing more than typing
sudo gem install contextr
h2. The basics In your code use
require 'rubygems'
require 'contextr'
and ContextR will be ready to use. h2. Demonstration of usage
require "rubygems"
require "contextr" 

class Person
  attr_accessor :name, :address, :university

  def initialize name, address, university
    self.name = name
    self.address = address
    self.university = university
  end

  def to_s
    "Name: #{name}"
  end
end

class University
  attr_accessor :name, :address

  def initialize name, address
    self.name = name
    self.address = address
  end

  def to_s
    "Name: #{name}"
  end
end

class Person
  layer :address, :education

  address.post :to_s do | n |
    n.return_value += "; Address: #{address}"
  end

  education.post :to_s do | n |
    n.return_value += ";\n[Education] #{university}"
  end
end

class University
  layer :address

  address.post :to_s do | n |
    n.return_value += "; Address: #{address}"
  end
end


hpi = University.new( "Hasso-Plattner-Institut", "Potsdam" )
somePerson = Person.new( "Gregor Schmidt", "Berlin", hpi )

puts 
puts somePerson
ContextR::with_layers :education do
  puts 
  puts somePerson

  ContextR::with_layers :address do
    puts 
    puts somePerson

    ContextR::without_layers :education do
      puts 
      puts somePerson
    end
  end
end
puts 
The above code prints:
Name: Gregor Schmidt

Name: Gregor Schmidt;
[Education] Name: Hasso-Plattner-Institut

Name: Gregor Schmidt; Address: Berlin;
[Education] Name: Hasso-Plattner-Institut; Address: Potsdam

Name: Gregor Schmidt; Address: Berlin
You may find other examples in the examples folder. h2. Other resources * "Rubyforge Project Page":http://rubyforge.org/projects/contextr * "Author's Development Blog - The Ruby Ahead":http://www.nach-vorne.de * "ContextR Statistics on ohloh":http://www.ohloh.net/projects/5037 h2. License This code is free to use under the same terms as Ruby h2. Contact Comments are welcome. Send an email to "Gregor Schmidt":mailto:ruby@schmidtwisser.de