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.

About Ashiq Alibhai

Ashiq Alibhai, PMP, has been a Rails aficionado since 2007, and developed web applications since early 2003, where he learned PHP in one summer. As the driving-force behind RailsRocket and the Launchpad project, he seeks to share the ease of development with Rails far and wide.
This entry was posted in Development and tagged , , . Bookmark the permalink.

Comments are closed.