Permalink
Browse files

add irb example

  • Loading branch information...
JoshCheek committed Jan 16, 2011
1 parent 83d36f4 commit 851a7409b0432204ee2934f54281b38ff8b1a374
Showing with 74 additions and 0 deletions.
  1. +1 −0 Readme.mdown
  2. +42 −0 irb/Readme.mdown
  3. +31 −0 irb/irb
View
@@ -10,6 +10,7 @@ Playgrounds
* [blackboardbot](http://github.com/JoshCheek/Play/tree/master/blackboardbot/) Script to log onto my school website, check all my forums for new posts, notify me of new posts by text message.
* [craigslist-watcher](http://github.com/JoshCheek/Play/tree/master/craigslist-watcher/) Script to check my local craigslist free page for the given item (in my case, free computers).
* [html-consolidated](http://github.com/JoshCheek/Play/tree/master/html-consolidated/) Use gcc's preparser to consolidate content into an html page (such as css and javascript libraries) so you can give someone a single html page with no external dependencies.
+* [irb](http://github.com/JoshCheek/Play/tree/master/irb/) Proof of concept for an interactive Ruby repl like irb.
* [mocking-the-internet](http://github.com/JoshCheek/Play/tree/master/mocking-the-internet/) Test libraries that rely on the internet by mocking the internet, and stubbing out various URLs.
* [physics-lab](http://github.com/JoshCheek/Play/tree/master/physics-lab/) Use the units-system gem to automate all the calculations I would have to do for my Physics lab.
* [ruby-s](http://github.com/JoshCheek/Play/tree/master/ruby-s/) How to use ruby's -s flag for simple command line args.
View
@@ -0,0 +1,42 @@
+Description
+===========
+
+Proof of concept for an interactive Ruby repl, like irb. I tried to add multilines by checking syntax with `$ ruby -c` but couldn't get it to work well, so removed it.
+
+Usage
+=====
+
+Takes arguments to set the prompt and result. Use "[ESCAPE]" for an escape character, current line is stored in $linenum. Embed Ruby with erb.
+
+ $ ./irb
+ $ ./irb -result='RESULT: '
+ $ ./irb -prompt='line:<%= $linenum %>: '
+ $ ./irb -result='[ESCAPE][31mRESULT: [ESCAPE][32m' -prompt='(<%= Time.now.strftime("%I:%M:%S %p") %> - Line:<%= $linenum %>)>> '
+
+---------------------------------------
+
+**This code is unmaintained.**
+
+_If you do something interesting with it, let me know so I can be happy._
+
+---------------------------------------
+
+Copyright (c) 2010 Joshua Cheek
+
+ Permission is hereby granted, free of charge, to any person obtaining a copy
+ of this software and associated documentation files (the "Software"), to deal
+ in the Software without restriction, including without limitation the rights
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the Software is
+ furnished to do so, subject to the following conditions:
+
+ The above copyright notice and this permission notice shall be included in
+ all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ THE SOFTWARE.
View
31 irb/irb
@@ -0,0 +1,31 @@
+#!/usr/bin/env ruby -s
+
+require 'erb'
+
+def to_prompt( prompt , rest='' )
+ ERB.new(prompt).result(TOPLEVEL_BINDING).gsub('[ESCAPE]',"\e") + rest + "\e[0m"
+end
+
+$linenum = 0
+$prompt ||= '[<%= $linenum %>]>> '
+$result ||= '=> '
+
+print to_prompt($prompt)
+
+while line = gets
+ line.chomp!
+ break if line == 'exit' || line == 'quit'
+
+ begin
+ result = eval( line , TOPLEVEL_BINDING , 'irb' , $linenum )
+ puts to_prompt( $result , result.inspect )
+ rescue Exception => e
+ puts e
+ puts
+ puts e.backtrace
+ ensure
+ $linenum += 1
+ print to_prompt($prompt)
+ end
+
+end

0 comments on commit 851a740

Please sign in to comment.