开发者

How to view/save/load work space in ruby's interactive mode

开发者 https://www.devze.com 2022-12-11 14:52 出处:网络
I need an interactive environment to play with some algorithm stuff. I want to be able to view what\'s been defined (data, function) so far and be able save/load so I can continue from a previous save

I need an interactive environment to play with some algorithm stuff. I want to be able to view what's been defined (data, function) so far and be able save/load so I can continue from a previous saved snapshot if somethi开发者_如何转开发ng went wrong. Since I chose ruby as my main scripting language, I hope it had these features built in.

If ruby interactive mode does not provide these functionality, what else you recommend for that?

Thanks


So here’s a technique that will append commands entered in your IRB session to a file in your home directory (idea from ruby-talk:58931). Put the following in your .irbrc:

module Readline
  module History
    LOG = "#{ENV['HOME']}/.irb-history"

    def self.write_log(line)
      File.open(LOG, 'ab') {|f| f << "#{line}
"}
    end

    def self.start_session_log
      write_log("
# session start: #{Time.now}

")
      at_exit { write_log("
# session stop: #{Time.now}
") }
    end
  end

  alias :old_readline :readline
  def readline(*args)
    ln = old_readline(*args)
    begin
      History.write_log(ln)
    rescue
    end
    ln
  end
end

Readline::History.start_session_log


You should check out the sketches gem which let's you prototype code in a temporary file in your preferred editor. I don't think it supports snapshots.

In irb I would use it as follows:

>> sketch
# Write some code in an editor ...

# Lists sketches and their code
>> sketches

# Reopens the first sketch from above
>> sketch 1

If you want a more powerful interactive prototyping environment, see boson.

0

精彩评论

暂无评论...
验证码 换一张
取 消