fOOrth/sire.rb

86 lines
1.6 KiB
Ruby
Raw Normal View History

2014-09-11 05:38:05 +02:00
# coding: utf-8
# A Simple Interactive Ruby Environment
2014-11-10 17:48:34 +01:00
require 'readline' #YUK
2014-09-11 05:38:05 +02:00
require 'pp'
class Object
#Generate the class lineage of the object.
def classes
begin
2014-11-10 17:48:34 +01:00
result = ""
klass = self.instance_of?(Class) ? self : self.class
2014-09-11 05:38:05 +02:00
begin
2014-11-10 17:48:34 +01:00
result << klass.to_s
2014-09-11 05:38:05 +02:00
klass = klass.superclass
2014-11-10 17:48:34 +01:00
result << " < " if klass
2014-09-11 05:38:05 +02:00
end while klass
2014-11-10 17:48:34 +01:00
result
2014-09-11 05:38:05 +02:00
end
end
end
class SIRE
#Set up the interactive session.
def initialize
2014-10-15 18:27:05 +02:00
@_done = false
2014-09-11 05:38:05 +02:00
@running = false
puts "Welcome to a Simple Interactive Ruby Environment\n"
puts "Use command 'q' to quit.\n\n"
end
#Quit the interactive session.
def q
2014-10-15 18:27:05 +02:00
@_done = true
2014-09-11 05:38:05 +02:00
puts
"Bye bye for now!"
end
2014-10-15 18:27:05 +02:00
#Load and run a file
def l(file_name)
@_break = false
lines = IO.readlines(file_name)
lines.each do |line|
exec_line(line)
return if @_break
end
"End of file '#{file_name}'."
end
#Execute a single line.
def exec_line(line)
result = eval line
pp result unless line.length == 0
rescue Interrupt => e
@_break = true
puts "\nExecution Interrupted!"
puts "\n#{e.class} detected: #{e}\n"
puts e.backtrace
puts "\n"
rescue Exception => e
@_break = true
puts "\n#{e.class} detected: #{e}\n"
puts e.backtrace
puts
end
2014-09-11 05:38:05 +02:00
#Run the interactive session.
def run_sire
2014-10-15 18:27:05 +02:00
until @_done
@_break = false
exec_line(Readline.readline('SIRE>', true))
2014-09-11 05:38:05 +02:00
end
puts "\n\n"
end
end