- __object__
- autoimport
- autoreload
- autoreload_files
- cache
- chain
- daemonize
- event
- hook
- meta
- meta
- method!
- nack
- new
- null
- prototype
- ref
- timed
Binds a method to Object/Kernel before calling it.
obj.__object__.id obj.__object__.class
For metaprogramming this can be important.
[ show source ]
# File lib/more/facets/reflection.rb, line 92 def __object__ @_object_reflection ||= ObjectReflection.new(self) end
Calls Object.autoimport
[ show source ]
# File lib/more/facets/capsule.rb, line 254 def autoimport(mod, file) Object.autoimport(mod, file) end
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.
[ show source ]
# 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
Same as autoreload, but does not include previously loaded features. This is equivalent to as adding a ‘false’ parameter to autoreload.
[ show source ]
# File lib/more/facets/autoreload.rb, line 112 def autoreload_files( *args ) autoreload( false, *args ) end
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.
[ show source ]
# 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
[ show source ]
# File lib/more/facets/chain.rb, line 54 def chain Chain.new(self) end
Turns the current script into a daemon process that detaches from the console. It can be shut down with a TERM signal.
[ show source ]
# 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
[ show source ]
# File lib/more/facets/eventhook.rb, line 84 def event(sym) if $!.name == sym yield $!.call end end
[ show source ]
# File lib/more/facets/eventhook.rb, line 81 def hook(sym) callcc{ |c| raise EventHook.new(sym, c) } end
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
[ show source ]
# 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
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
[ show source ]
# 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
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"
[ show source ]
# File lib/more/facets/1stclassmethod.rb, line 94 def method!(s) ( @__methods__ ||= {} )[s] ||= method(s) end
This is a light version of NackClass intended for minor usecases. See mega/nack for a complete version.
[ show source ]
# File lib/more/facets/nackclass.rb, line 38 def nack NackClass.new end
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"
[ show source ]
# File lib/more/facets/prototype.rb, line 83 def new(o=nil) return o.clone if o return clone end
[ show source ]
# File lib/more/facets/nullclass.rb, line 51 def null NullClass.new end
[ show source ]
# File lib/more/facets/prototype.rb, line 71 def prototype(&block) Prototype.new(&block) end
Shortcut reference constructor.
[ show source ]
# File lib/more/facets/reference.rb, line 78 def ref(x) Reference.new(x) end
Takes a block and returns the total time it took to execute.
[ show source ]
# File lib/more/facets/timer.rb, line 268 def timed yield( timer = Timer.new.start ) return timer.total_time end