Skip to content

Instantly share code, notes, and snippets.

@nickmccurdy
Last active December 12, 2015 02:09
Show Gist options
  • Save nickmccurdy/4696651 to your computer and use it in GitHub Desktop.
Save nickmccurdy/4696651 to your computer and use it in GitHub Desktop.
A simple REPL in Ruby
# REPL: Read Evaluate Print Loop
# See http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop
# The prompt for the REPL
PROMPT = 'repl> '
# Keep reading, evaluating, and printing forever (or until the user exits)
loop do
# Display the prompt
print PROMPT
# Grab the current line of input, setting the first word to the command
# variable and the rest of the arguments to the args variable
input = gets.chomp
command, *args = input.split(/\s/)
# Depending on what the command is, do different things and print the results
case command
when 'echo' then puts args.join ' '
when 'exit' then exit
when 'greet' then puts 'Hello, world!'
when 'help' then puts 'Commands: echo, exit, greet, help, time'
when 'time' then puts "It is now #{Time.now}."
else puts 'Invalid command' unless command.nil?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment