From: "Jörg W Mittag" Date: 2009-02-26T05:19:14+09:00 Subject: Re: Common Lisp conditions Gavin Sinclair wrote: > So far as the search engine on this newsgroup is concerned, there's > been no discussion about including a Common Lisp-style condition > system in a future version of Ruby. [...] > For those who _do_ know about Common Lisp conditions, is there any > appetite for them in Ruby? I see great positives and no negatives. > They don't seem at all un-Ruby-like; in fact the relationship between > high- and low-level code reminds me of the elegance of blocks and > yield. They seem to work very well in Ola Bini's new language Ioke (), which borrows heavily from Ruby. So, I agree: they would fit very well. Replacing Exceptions with Conditions could probably even be done in a (somewhat) backwards-compatible way. Maybe they could replace throw/catch, too. > After a small amount of thought, I haven't come up with a suitable > syntax. That's probably because I haven't used the things, only read > about them. I just tried converting the example from Practical Common Lisp, but it isn't too pretty. I hope that's because I mis-translated the original procedural Common Lisp into pseudo-OO Ruby and not because my chosen syntax is ugly. class MalformedLogEntryError < RuntimeError def initialize text @text = text end attr_reader :text end class LogEntryParser def parse_log_entry text error MalformedLogEntryError.new(text) unless text.wellformed_log_entry? LogEntry.new( ... ) restart use_value(value) value restart reparse_entry(fixed_text) parse_log_entry fixed_text end end class LogfileParser def parse_log_file file File.each_line(file).map do |line| begin @log_entry_parser.parse_log_entry text restart skip_log_entry nil end end end end class LogAnalyzer def main find_all_logs.each do |log| analyze_log log end handle MalformedLogEntryError restart skip_log_entry end def analyze_log log @logfile_parser.parse_log_file(log).each do |entry| analyze_entry entry end end end class StrictLogAnalyzer # Annotates malformed entries instead of ignoring def main super handle MalformedLogEntryError => c MalformedLogEntry.new(c.text) end end jwm