# encoding utf-8 module DSL link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article" def doit_with(some, parameters) # does it end def doit_with(mandatory, optional=nil, parameters=nil) # does it end def doit_with(mandatory, *optional_paramters) # does it end def doit_with(options={:named => 'parameter'}) # does it end def doit_with(*args) options = args.pop if Hash === args.last name = args.first # [...] raise ArgumentError, 'Bad combination of parameters' unless args.size == 1 # does it end Twitter.configure do |config| config.consumer_key = YOUR_CONSUMER_KEY config.consumer_secret = YOUR_CONSUMER_SECRET end def doit_with(&explicit_block) explicit_block.call end def doit_with_implicit_block yield if block_given? end def doit_with_block_for_configuration yield self end def doit_with(&arity_sensitive_block) arity_sensitive_block.call 'foo', 'bar' if arity_sensitive_block.arity == 2 end Twitter.configure do consumer_key = YOUR_CONSUMER_KEY consumer_secret = YOUR_CONSUMER_SECRET end def doit_with(&instance_eval_block) if instance_eval_block.arity == 0 # could also be class_eval, module_eval self.instance_eval(&instance_eval_block) else instance_eval_block.call self end end Client.find_by_firstname_and_lastname_and_gender('uschi', 'mueller', :female) METHOD_PATTERN = /^find_by_/ def method_missing(method, *args, &block) if method.to_s =~ METHOD_PATTERN # finder magic else super end end def respond_to?(method) return true if method =~ METHOD_PATTERN super end class Client < ActiveRecord::Base has_one :address has_many :orders belongs_to :role end class << self def has_something # macro definition end end client.firstname = 'uschi' puts client.lastname def generate(name) self.class.send(:define_method, name){puts name} eval("def #{name}() puts '#{name}' end") def some_method(name) puts name end # [...] end # $ ruby simple_test.rb at_exit do # shut down code end # inherited, included, method_missing, method_added, method_removed, trace_var, set_trace_func, at_exit set_trace_func proc { |event, file, line, id, binding, classname| printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname } 'text'.blank? class Object def blank? nil? || (respond_to?(:empty?) && empty?) end end class Object def blank? nil? || (respond_to?(:empty?) && empty?) end unless method_defined?(:blank?) end module MyGem module CoreExtensions module Object def blank? respond_to?(:empty?) ? empty? : !self end end end end Object.send :include, MyGem::CoreExtensions::Object end