[#2367] Standard libraries — Dave Thomas <dave@...>

From ruby-dev summary:

60 messages 2004/02/11

[#2397] PATCH: deprecate cgi-lib, getopts, importenv, parsearg from standard library — Gavin Sinclair <gsinclair@...>

Index: cgi-lib.rb

15 messages 2004/02/12

[#2465] PATCH: OpenStruct#initialize to yield self — Gavin Sinclair <gsinclair@...>

This is a common approach I use to object initialization; I don't know

24 messages 2004/02/19

Re: [PATCH] Persistent IRB history

From: nobu.nokada@...
Date: 2004-02-07 01:58:20 UTC
List: ruby-core #2354
Hi,

At Sat, 7 Feb 2004 03:15:37 +0900,
Thomas Uehlinger wrote in [ruby-core:02352]:
> I found persistent Readline history (i.e. saved to a hidden
> file in the users home directory) a pretty useful feature in
> other applicatons.
> So here is a patch for IRB so the history gets automatically
> saved to ~/.irb_history and will be loaded next time IRB is
> used.

Sounds nice, but I guess it would be better if configurable.


Index: lib/irb/init.rb
===================================================================
RCS file: /cvs/ruby/src/ruby/lib/irb/init.rb,v
retrieving revision 1.6
diff -u -2 -p -d -r1.6 init.rb
--- lib/irb/init.rb	22 Nov 2002 05:10:42 -0000	1.6
+++ lib/irb/init.rb	7 Feb 2004 01:31:17 -0000
@@ -181,15 +181,18 @@ module IRB
   end
 
+  # enumerate possible rc files
+  def IRB.rc_files(rc)
+    yield File.expand_path("~/.irb#{rc}") if ENV.key?("HOME")
+    yield ".irb#{rc}"
+    yield "irb#{rc.sub(/\A_?/, '.')}"
+    yield "_irb#{rc}"
+    yield "$irb#{rc}"
+  end
+
   # running config
   def IRB.run_config
     if @CONF[:RC]
-      rcs = []
-      rcs.push File.expand_path("~/.irbrc") if ENV.key?("HOME")
-      rcs.push ".irbrc"
-      rcs.push "irb.rc"
-      rcs.push "_irbrc"
-      rcs.push "$irbrc"
       catch(:EXIT) do
-	for rc in rcs
+	rc_files("rc") do |rc|
 	  begin
 	    load rc
Index: lib/irb/input-method.rb
===================================================================
RCS file: /cvs/ruby/src/ruby/lib/irb/input-method.rb,v
retrieving revision 1.4
diff -u -2 -p -d -r1.4 input-method.rb
--- lib/irb/input-method.rb	23 Mar 2003 17:58:43 -0000	1.4
+++ lib/irb/input-method.rb	7 Feb 2004 01:55:54 -0000
@@ -86,4 +86,21 @@ module IRB
     class ReadlineInputMethod < InputMethod
       include Readline 
+
+      def ReadlineInputMethod.create_finalizer(hist, file)
+	proc do
+	  if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
+            if hf = IRB.conf[:HISTORY_FILE]
+	      file = File.expand_path(hf)
+            end
+            if file
+              open(file, 'w' ) do |f|
+                hist = hist.to_a
+                f.puts(hist[-num..-1] || hist)
+              end
+	    end
+	  end
+	end
+      end
+
       def initialize
 	super
@@ -92,4 +109,24 @@ module IRB
 	@line = []
 	@eof = false
+
+        loader = proc {|f| f.each {|l| HISTORY << l.chomp}}
+	if hist = IRB.conf[:HISTORY_FILE]
+	  hist = File.expand_path(hist)
+	  begin
+	    open(hist, &loader) 
+	  rescue
+	  end
+	else
+	  IRB.rc_files("_history") do |hist|
+	    begin
+	      open(hist, &loader)
+	    rescue
+	      hist = nil
+	    else
+	      break
+	    end
+	  end
+	end
+	ObjectSpace.define_finalizer(self, ReadlineInputMethod.create_finalizer(HISTORY, hist))
       end
 


-- 
Nobu Nakada

In This Thread