that book Built FasterCSV (now CSV), HighLine (with Greg), Elif, and some other scarier experiments Documented some of Ruby http://blog.grayproductions.net/
that book Built FasterCSV (now CSV), HighLine (with Greg), Elif, and some other scarier experiments Documented some of Ruby http://blog.grayproductions.net/ http://twitter.com/JEG2
C D].each do |name| eval <<-END_RUBY module #{name} def call puts "#{name}" super end end END_RUBY end ! class E < A include B include C include D def call puts "E" super end end ! E.new.call
"A" end end ! %w[B C D].each do |name| eval <<-END_RUBY module #{name} def call puts "#{name}" super end end END_RUBY end ! class E < A include B include C include D def call puts "E" super end end ! E.new.call
C D].each do |name| eval <<-END_RUBY module #{name} def call puts "#{name}" super end end END_RUBY end ! a = A.new a.extend(B) a.extend(C) a.extend(D) a.call
C D].each do |name| eval <<-END_RUBY module #{name} def call puts "#{name}" super end end END_RUBY end ! a = A.new a.extend(B) a.extend(C) a.extend(D) a.call D C B A
C D].each do |name| eval <<-END_RUBY module #{name} def call puts "#{name}" super end end END_RUBY end ! a = A.new a.extend(B) a.extend(C) a.extend(D) class << a def call puts "Invisible" super end end a.call
C D].each do |name| eval <<-END_RUBY module #{name} def call puts "#{name}" super end end END_RUBY end ! a = A.new a.extend(B) a.extend(C) a.extend(D) class << a def call puts "Invisible" super end end a.call Invisible D C B A
def logger $stdout end def log(message) logger.puts "%s: %s" % [ Time.now.strftime("%D %H:%M:%S"), message ] end end ! if __FILE__ == $PROGRAM_NAME MiniLogger.log "Called as a module method and " + "written to $stdout." end
Whatever include MiniLogger def logger @logger ||= open("whatever.log", "w") end def initialize log "Called as an " + "instance method " + "and written to " + "a file." end end ! Whatever.new module MiniLogger extend self def logger $stdout end def log(message) logger.puts "%s: %s" % [ Time.now.strftime("%D %H:%M:%S"), message ] end end ! if __FILE__ == $PROGRAM_NAME MiniLogger.log "Called as a module method and " + "written to $stdout." end
class BaseError < RuntimeError; end def self.const_missing(error_name) if error_name.to_s =~ /\wError\z/ const_set(error_name, Class.new(BaseError)) else super end end end ! p Errors::SaveError
require "ostruct" ! # These extra methods are mixed into the OpenStruct stored in Config. module Configured # This method loads configuration settings from a plain Ruby file. def update_from_config_file(path = config_file) eval <<-END_UPDATE config = self #{File.read(path)} config END_UPDATE end # ... end # This constant holds all global configuration, see Configured for details. Config = OpenStruct.new.extend(Configured)
module DoubleMixin module ClassMethods # ... end module InstanceMethods # ... end def self.included(receiver) receiver.extend(ClassMethods) receiver.send(:include, InstanceMethods) end end
self.extended(singleton) singleton.methods.grep(/\w!\z/). each do |bang| singleton.instance_eval <<-END_RUBY def #{bang} super self end END_RUBY end end end ! p "ruby".extend(SafelyChainable). strip!.capitalize! p "ruby".strip!. capitalize! ! # NoMethodError: # undefined method # `capitalize!' for # nil:NilClass