# IRB - ignoring comments (toggling)

**URL:** <https://www.ruby-forum.com/t/irb-ignoring-comments-toggling/117016>\
**Category:** Ruby\
**Created:** [October 12, 2007, 2:44am UTC](https://www.ruby-forum.com/t/irb-ignoring-comments-toggling/117016 "2007-10-12T02:44:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [October 12, 2007, 2:44am UTC](https://www.ruby-forum.com/t/irb-ignoring-comments-toggling/117016/1 "2007-10-12T02:44:34Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [October 12, 2007, 4:42am UTC](https://www.ruby-forum.com/t/irb-ignoring-comments-toggling/117016/2 "2007-10-12T04:42:42Z")

</div>

> 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](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](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 B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com/](http://giles.tumblr.com/)
