# Ruby standard library extensions **ruby-ext** is a collection of various utility classes and standard library extensions for the Ruby language. ## must - assertion tool, kind of RSpec assertions in runtime code 1.must_be.in 1..2 "a".must_be.in 'a', 'b', 'c' key.must_be.a String value.must_not_be.nil must_be.never_called a.must_be.== b ## inherit - combines include & extend in one Do you remember this *def self.included(base) ... end* hack? You don't need it anymore. module Feature def cool_method; end module ClassMethods def cool_class_method; end end end class TheClass inherit Feature end TheClass.new.cool_method TheClass.cool_class_method ## cache_method & synchronize_method def complex_calculation 2 * 2 end cache_method :complex_calculation def money_transfer amount @from -= amount @to += amount end synchronize_method :money_transfer ## OpenConstructor - adds mass assignment to any class class TheClass include OpenConstructor attr_accessor :a, :b end o = TheClass.new.set :a => 'a', :b => 'b' o.a => 'a' o.to_hash => {:a => 'a', :b => 'b'} ## VResource - Auto ClassLoader, forget about the 'require' Use direct mapping between your code and filesystem, define your class in the "/TheNamespace/.../TheClass.rb" file. And then just name it and ClassLoader will find and load it automatically. # A/B/C.rb # # class C # end # A::B::C.new In the C.rb script for the C class, we write just the *class C*, instead of all *module A; module B; class C*. There are no really *A* and *B* modules, they are generated on the fly. ## Micon - Micro Container, assembles and manages pieces of your application Very small and handy [IoC][ioc] container with scopes and bijections (injection + outjection), borrowed from JBoss Seam. class Logger scope :application end class Session scope :request end class Application inject :session => Session, :logger => Logger def do_business session[:key] = 'value' logger.log 'well done' end end Micon.activate :request, {} do Application.new.do_business end ## More These are just a small part of all handy methods and extensions, for more details please go to specs. # Usage sudo gem install ruby-ext require 'ruby_ext' If you also need ClassLoader and Micon add following: require 'vresource' VResource.hook! require 'micon' Micon::Managed.hook! Module Copyright (c) 2009 Alexey Petrushin [http://bos-tec.com](http://bos-tec.com), released under the MIT license. [ioc]: http://en.wikipedia.org/wiki/Inversion_of_control