Permalink
Browse files

Fixing a whole series of things.

  • Loading branch information...
1 parent b03f172 commit 8749512c15de36d5aebabace4ccee1f6c122f46e @davidrichards committed Mar 1, 2009
View
@@ -178,6 +178,12 @@ From command line:
=> #<Sirb::Runner:0x23df728 @block=proc {|x,y| x + y}, @name="add", @description="This is my general-use adder, that adds a set of numbers">
>> commands
=> []
+
+=== Functional-style Programming
+
+Sometimes it is useful to have a bit of functional flair when implementing equations. I've slurped up a bit of that and included it:
+
+ .... Fill these in
== Known Issues
View
@@ -1,9 +1,8 @@
#!/usr/bin/env ruby -wKU
-require File.join(File.dirname(__FILE__), %w(.. lib version))
require 'yaml'
version_hash = YAML.load_file(File.join(File.dirname(__FILE__), %w(.. VERSION.yml)))
-version = [version_hash[:major].to_s, version_hash[:minor].to_s, version_hash[:patch].to_s].join(":")
+version = [version_hash[:major].to_s, version_hash[:minor].to_s, version_hash[:patch].to_s].join(".")
sirb_file = File.join(File.dirname(__FILE__), %w(.. lib sirb))
stored_procedures_file = File.join(File.dirname(__FILE__), %w(.. lib stored_procedures))
View
@@ -53,7 +53,7 @@ def archive_method(target, new_name=nil)
# Access instance methods with array notation. Returns UnboundMethod,
alias [] instance_method
-
+
# Define a instance method with name sym and body f.
# Example: String[:backwards] = lambda { reverse }
def []=(sym, f)
View
@@ -3,33 +3,34 @@
module Sirb
end
-require 'sirb/loader'
+require 'sirb/lib_loader'
Dir.glob("#{File.dirname(__FILE__)}/overrides/*.rb").each { |file| require file }
require 'rubygems'
-Loader.add_lib('rubygems')
-Loader.add_lib('mathn')
-Loader.add_lib('set')
-Loader.add_lib('matrix')
-Loader.add_lib('narray')
-Loader.add_lib('rnum')
-Loader.add_lib('rgl') {
+LibLoader.add_lib('rubygems')
+LibLoader.add_lib('mathn')
+LibLoader.add_lib('set')
+LibLoader.add_lib('matrix')
+LibLoader.add_lib('narray')
+LibLoader.add_lib('rnum')
+LibLoader.add_lib('rgl') {
require 'rgl/dot'
require 'rgl/adjacency'
require 'rgl/traversal'
require 'rgl/topsort'
}
-Loader.add_lib('rbtree')
-Loader.add_lib('statisticus')
+LibLoader.add_lib('tenacious_g')
+LibLoader.add_lib('rbtree')
+LibLoader.add_lib('statisticus')
-Loader.add_lib('general statistics') {
+LibLoader.add_lib('general statistics') {
require 'sirb/general_statistics'
include Sirb::GeneralStatistics
}
-Loader.add_lib('enumerable statistics') {
+LibLoader.add_lib('enumerable statistics') {
require 'sirb/enumerable_statistics'
class Array
include Sirb::EnumerableStatistics
@@ -44,4 +45,4 @@ class Array
}
-puts Loader.to_s if Loader.to_s
+puts LibLoader.to_s if LibLoader.to_s
View
@@ -85,8 +85,8 @@ def apply_tail(*last)
# Here are operator alternatives for these methods. The angle brackets
# point to the side on which the argument is shifted in.
- alias >> apply_head # g = f >> 2 -- set first arg to 2
- alias << apply_tail # g = f << 2 -- set last arg to 2
+ # alias >> apply_head # g = f >> 2 -- set first arg to 2
+ # alias << apply_tail # g = f << 2 -- set last arg to 2
# Return a new lambda that caches the results of this function and
# only calls the function when new arguments are supplied.
View
@@ -0,0 +1,45 @@
+class LibLoader
+ class << self
+ def libs
+ @libs ||= {}
+ end
+
+ def add_lib(name, &block)
+ name = name.to_s
+ block ||= lambda{require name}
+ self.libs[name] = self.safe_require(&block)
+ end
+
+ def all_libs
+ self.libs.keys.sort
+ end
+
+ def libs_loaded
+ self.libs.map {|k, v| k if v }.compact
+ end
+
+ def failed_libs
+ self.libs.map {|k, v| k unless v }.compact
+ end
+
+ def safe_require(&block)
+ begin
+ block.call
+ # If the lib is already loaded, it may return false, but it's available.
+ # If there's a problem with this, you should require the lib in a block
+ # and raise an exception if it fails.
+ true
+ # If the laod returns false, then
+ rescue Exception => e # Very important
+ false
+ end
+ end
+
+ def to_s
+ return nil if self.failed_libs.empty?
+ "Libs loaded:\n\t" + (self.libs_loaded.empty? ? "None" : self.libs_loaded.join(", ")) +
+ "\nLibs NOT loaded:\n\t" + (self.failed_libs.empty? ? "None" : self.failed_libs.join(", "))
+ end
+ end
+
+end
View
@@ -58,6 +58,7 @@ def commands_as_commands
protected :commands_as_commands
def find(name)
+ return nil unless name
coerce name
begin
pstore.transaction { pstore[@name] }
@@ -105,12 +106,12 @@ def sirb_description
This is Irb, with some extra libraries and commands loaded.
You have loaded the following libraries:
}
- result += tab_indent( Loader.libs_loaded.join("\n") )
+ result += tab_indent( LibLoader.libs_loaded.join("\n") )
result += "\n\n"
- if not Loader.failed_libs.empty?
+ if not LibLoader.failed_libs.empty?
result += "The following libraries are not available on your system at this time:\n"
- result += tab_indent( Loader.failed_libs.join("\n") )
+ result += tab_indent( LibLoader.failed_libs.join("\n") )
result += "\n\n"
end
@@ -242,20 +243,25 @@ def store_command(name, l, description=nil)
# May want to move this to delegate (standard library)
# http://www.ruby-doc.org/stdlib/libdoc/delegate/rdoc/index.html
def method_missing(sym, *args, &block)
- base, extension = breakdown_method(sym)
- if Runner.find(base) and extension == 'methodize'
- Runner.methodize(base)
- elsif Runner.find(base.to_sym) and extension == 'to_s'
- Runner.stringify_proc(base)
- elsif Runner.find(sym)
+ # base, extension = breakdown_method(sym)
+ # if Runner.find(base) and extension == 'methodize'
+ # Runner.methodize(base)
+ # elsif Runner.find(base) and extension == 'to_s'
+ # require 'rubygems'
+ # require 'ruby-debug'
+ # debugger
+ # Runner.stringify_proc(base)
+ if Runner.find(sym)
Runner.run(sym, *args)
+ # elsif Runner.respond_to?(sym)
elsif Runner.respond_to?(sym)
Runner.send(sym, *args, &block)
else
super
end
end
+ # Doesn't work.
def breakdown_method(sym)
s = sym.to_s
r = /^(.+)\.(.+)$/
Oops, something went wrong.

0 comments on commit 8749512

Please sign in to comment.