Methods
Public Instance methods
__object__()

Binds a method to Object/Kernel before calling it.

  obj.__object__.id
  obj.__object__.class

For metaprogramming this can be important.

# File lib/more/facets/reflection.rb, line 92
  def __object__
    @_object_reflection ||= ObjectReflection.new(self)
  end
autoimport(mod, file)

Calls Object.autoimport

# File lib/more/facets/capsule.rb, line 254
  def autoimport(mod, file)
    Object.autoimport(mod, file)
  end
autoreload( *args )

Autoreload feature files.

Automatically reload, at regular intervals, any previously loaded features, and/or other files not already loaded, if they have been modified since the last interval check. A numeric parameter sets the reload interval in seconds and the file parameter can either be a glob string or an array of file paths. If a glob string, it is expanded only once on the initial method call. Supplying a boolean parameter of ‘false’ will force autreload to skip previously loaded features and only reload the specified files. Also keeps a "dirty" flag.

# File lib/more/facets/autoreload.rb, line 50
  def autoreload( *args )

    check_interval=10
    include_features = true
    files = nil

    args.each do |arg|
      case arg
      when Numeric
        check_interval = arg
      when String
        files = Dir.glob( arg )
      when Array
        files = arg
      when TrueClass, FalseClass
        include_features = arg
      end
    end

    file_mtime = {}

    Thread.new(Time.now) do |start_time|
      loop do
        sleep check_interval

        if include_features
          feature_files = $LOADED_FEATURES.collect { |feature|
            $LOAD_PATH.each { |lp| file = File.join(lp, feature) }
          }.flatten

          feature_files.each { |file|
            if File.exists?(file) and (mtime = File.stat(file).mtime) > (file_mtime[file] || start_time)
              $autoreload_dirty = true
              file_mtime[file] = mtime
              STDERR.puts "File '#{ file }' reloaded"
              begin
                load(file)
              rescue Exception => e
                STDERR.puts e.inspect
              end
            end
          }
        end

        if files
          files.each do |file|
            if File.exists?(file) and (mtime = File.stat(file).mtime) > (file_mtime[file] || start_time)
              $autoreload_dirty = true
              file_mtime[file] = mtime
              STDERR.puts "File '#{ file }' changed"
            end
          end
        end

      end
    end

  end
autoreload_files( *args )

Same as autoreload, but does not include previously loaded features. This is equivalent to as adding a ‘false’ parameter to autoreload.

# File lib/more/facets/autoreload.rb, line 112
  def autoreload_files( *args )
    autoreload( false, *args )
  end
cache(m = nil)

Object#cache is essentially like Module#memoize except it can also be used on singleton/eigen methods. OTOH, memoize‘s implementation is arguably better for it‘s use of bind instead of alias. Eventually the two implmenations will be reconciled with a single implmentation.

# File lib/more/facets/memoize.rb, line 97
  def cache m = nil
    if m
      (Module === self ? self : (class << self; self; end)).module_eval "alias_method '__\#{ m }__', '\#{ m }'\ndef \#{ m }(*__a__,&__b__)\nc = cache['\#{ m }']\nk = [__a__,__b__]\nif c.has_key? k\nc[k]\nelse\nc[k] = __\#{ m }__(*__a__,&__b__)\nend\nend\n"
    end
    @cache ||= Hash::new{|h,k| h[k]={}}
  end
chain()
# File lib/more/facets/chain.rb, line 54
  def chain
    Chain.new(self)
  end
daemonize()

Turns the current script into a daemon process that detaches from the console. It can be shut down with a TERM signal.

# File lib/more/facets/daemonize.rb, line 31
  def daemonize
    exit if fork                   # Parent exits, child continues.
    Process.setsid                 # Become session leader.
    exit if fork                   # Zap session leader. See [1].
    Dir.chdir "/"                  # Release old working directory.
    File.umask 0000                # Ensure sensible umask. Adjust as needed.
    STDIN.reopen "/dev/null"       # Free file descriptors and
    STDOUT.reopen "/dev/null", "a" # point them somewhere sensible.
    STDERR.reopen STDOUT           # STDOUT/ERR should better go to a logfile.
    trap("TERM") { exit }
  end
event(sym) {|| ...}
# File lib/more/facets/eventhook.rb, line 84
  def event(sym)
    if $!.name == sym
      yield
      $!.call
    end
  end
hook(sym)
# File lib/more/facets/eventhook.rb, line 81
  def hook(sym)
    callcc{ |c| raise EventHook.new(sym, c) }
  end
meta()

Provides access to an object‘s metaclass (ie. singleton) by-passsing access provisions. So for example:

  class X
    meta.attr_accesser :a
  end

  X.a = 1
  X.a #=> 1

The fact that some class methods are private and thus inaccessable via use of metaclass, is really a problem with them being private —which is probably not really neccessary. Short of making them public though, thiere is this method or one could use the instance functor.

TODO: Don‘t see any reason this can‘t go into core, but there is a conflict with URI prior to 1.8.6, so it will have to wait.

 CREDIT: Trans
# File lib/more/facets/kernel/meta.rb, line 25
  def meta
    @_meta_functor ||= Functor.new do |op,*args|
      (class << self; self; end).send(op,*args)
    end
  end
meta()

Provides access to an object‘s metaclass (ie. singleton) by-passsing access provisions. So for example:

  class X
    meta.attr_accesser :a
  end

  X.a = 1
  X.a #=> 1

The fact that some class methods are private and thus inaccessable via use of metaclass, is really a problem with them being private —which is probably not really neccessary. Short of making them public though, thiere is this method or one could use the instance functor.

TODO: Don‘t see any reason this can‘t go into core, but there is a conflict with URI prior to 1.8.6, so it will have to wait.

 CREDIT: Trans
# File lib/more/facets/kernel/meta.rb, line 25
  def meta
    @_meta_functor ||= Functor.new do |op,*args|
      (class << self; self; end).send(op,*args)
    end
  end
method!(s)

Easy access to method as objects, and they retain state!

  def hello
    puts "Hello World!"
  end

  m1 = method!(:hello)   #=> <Method: #hello>

  def m1.annotate
    "simple example"
  end

  m2 = method!(:hello)
  m2.annotate  #=> "simple example"
# File lib/more/facets/1stclassmethod.rb, line 94
  def method!(s)
    ( @__methods__ ||= {} )[s] ||= method(s)
  end
nack()

This is a light version of NackClass intended for minor usecases. See mega/nack for a complete version.

# File lib/more/facets/nackclass.rb, line 38
  def nack
    NackClass.new
  end
new(o=nil)

Synonymous with clone, this is an interesting method in that it promotes prototype-based Ruby. Now Classes aren‘t the only things that respond to new.

  "ABC".new  => "ABC"
# File lib/more/facets/prototype.rb, line 83
  def new(o=nil)
    return o.clone if o
    return clone
  end
null()
# File lib/more/facets/nullclass.rb, line 51
  def null
    NullClass.new
  end
prototype(&block)
# File lib/more/facets/prototype.rb, line 71
  def prototype(&block)
    Prototype.new(&block)
  end
ref(x)

Shortcut reference constructor.

# File lib/more/facets/reference.rb, line 78
  def ref(x)
    Reference.new(x)
  end
timed() {|timer = Timer.new.start| ...}

Takes a block and returns the total time it took to execute.

# File lib/more/facets/timer.rb, line 268
  def timed
    yield( timer = Timer.new.start )
    return timer.total_time
  end