IRB - ignoring comments (toggling)

Discussion in 'Ruby' started by Marc Heiler, Oct 12, 2007.

  1. Marc Heiler

    Marc Heiler Guest

    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 :)
    --
    Posted via http://www.ruby-forum.com/.
     
    Marc Heiler, Oct 12, 2007
    #1
    1. Advertising

  2. > 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/
     
    Giles Bowkett, Oct 12, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?c2lhag==?=

    Toggling Image

    =?Utf-8?B?c2lhag==?=, May 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    428
    =?Utf-8?B?U2hhbmU=?=
    May 6, 2005
  2. Sam Stephenson
    Replies:
    1
    Views:
    228
    Andrew Walrond
    Jun 18, 2005
  3. Replies:
    1
    Views:
    161
    Florian GroƟ
    Oct 26, 2005
  4. anne001
    Replies:
    1
    Views:
    276
    anne001
    Jun 27, 2006
  5. RichardOnRails
    Replies:
    11
    Views:
    212
    RichardOnRails
    Sep 7, 2008
Loading...

Share This Page