Skip to content

Instantly share code, notes, and snippets.

@dmmfll
Last active January 18, 2017 19:04
Show Gist options
  • Save dmmfll/6e5f677c00ec7dc462b69ebbc4f29999 to your computer and use it in GitHub Desktop.
Save dmmfll/6e5f677c00ec7dc462b69ebbc4f29999 to your computer and use it in GitHub Desktop.
README.rst
convert_rst_to_md
gist_url
.ipynb_checkpoints/
Untitled.*
comments.*

REPL game

attributes

  • read
  • evaluate
  • print
  • loop

Minimal function.

./repl_it_simple.rb is a template you can use to create a REPL in Ruby.

This template adds an infinite looping feature until the word quit is entered.

It includes all the attributes of a REPL.

More function.

./repl_it.rb is a template you can use to create a REPL in Ruby.

This template adds an infinite looping feature until a certain key word is entered.

The template also introduces a new concept: exception handling.

When a user presses control c while your script is running it will exit a bit more gracefully.

Now that you know what methods are, you can write your own methods to further process the input.

Resources

#!/usr/bin/env ruby
def exit_words
['exit', 'quit', 'q', 'x', 'bye', 'goodbye']
end
def continue? word
not exit_words.include? word
end
def do_stuff_to(input)
puts "\nYour response was '#{input}'.\n"
unless continue? input
puts "Good bye."
end
end
input = ""
catch :ctrl_c do
begin
while continue? input
puts "\nEnter your response.\n#{exit_words} or ^C to exit:\n"
input = gets.chomp
do_stuff_to(input)
end
rescue Exception
puts "\nUser interrupted."
end
end
#!/usr/bin/env ruby
def do_stuff_to(input)
puts "\nYour response was '#{input}'.\n"
end
input = ""
while input != "quit"
puts "\nEnter your response.\n'quit' or ^C to exit:\n"
input = gets.chomp
do_stuff_to(input)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment