citizen428.blog()

Try to learn something about everything

A Poor Man's REPL in Ruby

I love all the little discussions on RubyLearning, especially when they lead to me writing stupid code the world doesn’t need. ;-) Today: a poor man’s REPL in 2 lines of Ruby 1.9:

1
2
repl = -> prompt { print prompt; puts(" => %s" % eval(gets.chomp!)) }
loop { repl[">> "] }

Behold the wonder:
1
2
3
4
5
6
7
8
>> 2 + 3
 => 5
>> "foo".upcase
 => FOO
>> [].class
 => Array
>> %w(foo bar)[0]
 => foo

Comments