IRB - ignoring comments (toggling)

M

Marc Heiler

Hi,

i think i have asked this ~a year or so ago already,
roughly (and in a much shorter form). This is my last try at
it, it is in my todo list, but I now want to either
solve it, or remove it from my todo list :)


My basic wish is the following:

Since i sometimes copy-paste commented code straight
into irb, I want to toggle "comment reading" on and
off, so i can text long snippets of commented
code from some .rb file straight into IRB to
see what changes i did, but without removing the
leading # comments. I want IRB to do that
comment-removing part.

Specifically, before I copy paste a lot of ruby code
into irb, i would like to do something toggle-able, like

toggle_comments()

which would then strip all leading #'s before it is interpreted
~similar to a regex such as:

x = STDIN.gets.gsub(/^[\s#]*/, '')

So basically, I would just like a method like

def toggle_comments
STDIN.gets.gsub(/^[\s#]*/, '')
end


I looked inside lib/ruby/1.8/irb, it is a little bit
complicated (and almost no comments).
Inside
context.rb

I found an @io object that is like so:
@io = ReadlineInputMethod.new
@io = StdioInputMethod.new
(Depends on whether the user has readline, or not.)

Theses classes can be found in:
input-method.rb

And the @io seems to be interpreted by:
ruby-lex.rb


The basic read-in seems to be done via @io.gets and is
then stored in @io.
I looked inside input-method.rb and ruby-lex.rb, but I am kinda at
a loss now ... does anyone know how to manipulate the @io
variable or whatever else is used for storing the "string that
the user typed"?
Should I extend class ReadlineInputMethod with a toggle_comment
and use that regex, or maybe there exists some other, easier way
to apply filtering on that IRB @io before it gets interpreted?

Thanks for the reading, I am glad this thing will soon be
out of my todo list either way :)
 
G

Giles Bowkett

Since i sometimes copy-paste commented code straight
into irb, I want to toggle "comment reading" on and
off, so i can text long snippets of commented
code from some .rb file straight into IRB to
see what changes i did, but without removing the
leading # comments. I want IRB to do that
comment-removing part.

I would do this by applying a regular expression to the contents of
the clipboard itself, but I'm on OS X, which makes it easy to get to
the clipboard. If you're on OS X use this code:

http://pastie.caboo.se/106429

And add a regex.

An alternative way to do it would be to use a text editor
interactively from within irb:

http://pastie.caboo.se/106430

This allows you to use your favorite text editor to edit code which
irb will then eval as normal Ruby code. The benefit is with large text
blocks where you might want to use regexes, or if you're hacking on
code which you'll want to save as soon as you've got it working.
Obviously you could use the regex features within your text editor to
remove the # characters. (This assumes you're on a Unix system of some
kind.)

All this code goes in your .irbrc file.

--
Giles Bowkett

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top