Ruby Interactive Prompt
I just discovered a couple of days ago that this amazing online ruby interactive prompt is a 200,000+ line monster of code! And, more importantly, it’s down.
I never understood how to write a prompt like that; a little experimentation resulted in this code, which works (more or less) the same (minus the demo/tutorial material):
def show_prompt
putc "\n" # blank line improves readability
putc ">"
putc " "
end
show_prompt
input = gets
while(true)
begin
puts eval(input)
rescue StandardError => e
puts "Error: " + e
end
show_prompt
input = gets
end
Easy as that! And, as you’d expect, everything stays in scope, so you can do whatever you please.
Tags: code snippets, interactive prompt, ruby Posted in


Leave a Reply