"Filing out" irb prototyping sessions?

Hi folks,

It looks like you can use irb as a rapid prototyping environment by
combining ability to bind to objects and modify them, utilize tab
completion, and of course interact/prototype with these objects you are
manipulating.

Suppose you prototype some things and would like to “file out” your
changes to Ruby code so that you could easily reference your ideas.

Is there some way to easily file out object definitions from an irb
session?

On Dec 5, 2006, at 2:15 PM, Griff wrote:

Is there some way to easily file out object definitions from an irb
session?

There is an excellent collection of history methods for IRb at:

http://blog.bleything.net/articles/2006/10/21/shell-style-history-for-
irb

Unfortunately, that link seems to be down right now. The following
code is my .irbrc file, which includes the methods. (It uses the
wirble gem.)

James Edward G. II

load libraries

require ‘rubygems’
require ‘wirble’

start wirble (with color)

Wirble.init
Wirble.colorize

history additions from http://blog.bleything.net/

def history(how_many = 50)
history_size = Readline::HISTORY.size

no lines, get out of here

puts “No history” and return if history_size == 0

start_index = 0

not enough lines, only show what we have

if history_size <= how_many
how_many = history_size - 1
end_index = how_many
else
end_index = history_size - 1 # -1 to adjust for array offset
start_index = end_index - how_many
end

start_index.upto(end_index) {|i| print_line i}
nil
end
alias :h :history

-2 because -1 is ourself

def history_do(lines = (Readline::HISTORY.size - 2))
irb_eval lines
nil
end
alias :h! :history_do

def history_write(filename, lines)
file = File.open(filename, ‘w’)

get_lines(lines).each do |l|
file << “#{l}\n”
end

file.close
end
alias :hw :history_write

private
def get_line(line_number)
Readline::HISTORY[line_number]
end

def get_lines(lines = [])
return [get_line(lines)] if lines.is_a? Fixnum

out = []

lines = lines.to_a if lines.is_a? Range

lines.each do |l|
out << Readline::HISTORY[l]
end

return out
end

def print_line(line_number, show_line_numbers = true)
print "[%04d] " % line_number if show_line_numbers
puts get_line(line_number)
end

def irb_eval(lines)
to_eval = get_lines(lines)

eval to_eval.join(“\n”)

to_eval.each {|l| Readline::HISTORY << l}
end

On Dec 5, 2006, at 12:15 , Griff wrote:

Is there some way to easily file out object definitions from an irb
session?

Courtesy Ryan D.:

first: gem install ruby2ruby

require ‘rubygems’
require ‘discover’
require ‘ruby2ruby’
$old_classes = discover_classes
def saveirb
(discover_classes - $old_classes).each do |klass|
puts RubyToRuby.translate(klass)
end
end


Eric H. - [email protected] - http://blog.segment7.net

I LIT YOUR GEM ON FIRE!