Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Last active July 18, 2018 04:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JoshCheek/baab86f3258ff98c468f3dbc032dc9c5 to your computer and use it in GitHub Desktop.
Save JoshCheek/baab86f3258ff98c468f3dbc032dc9c5 to your computer and use it in GitHub Desktop.
Write a REPL in Ruby
# Part of a post here: https://medium.com/@josh.cheek/c45dac057a1a
ruby -e 'loop { p eval gets }'
ruby -e '
bnd = binding() # gets a new binding
loop { p bnd.eval gets } # eval within the same binding each time
'
ruby -r readline -e '
bnd = binding()
loop do
input = Readline.readline("> ", true) # readline in place of gets
p bnd.eval input
end
'
ruby -r coderay -r readline -e '
bnd = binding()
loop do
input = Readline.readline("> ", true)
result = bnd.eval input
puts CodeRay.encode(result.inspect, :ruby, :terminal)
end
'
ruby -r coderay -r readline -e '
bnd = binding()
while input = Readline.readline("> ", true)
result = bnd.eval input
puts CodeRay.encode(result.inspect, :ruby, :terminal)
end
'
ruby -r coderay -r readline -e '
bnd = binding()
while input = Readline.readline("> ", true)
begin
result = bnd.eval input
puts CodeRay.encode(result.inspect, :ruby, :terminal)
rescue StandardError => e
puts "\e[31m#{e.class}:\e[0m #{e.message}"
end
end
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment