# "Filing out" irb prototyping sessions?

**URL:** <https://www.ruby-forum.com/t/filing-out-irb-prototyping-sessions/78436>\
**Category:** Ruby\
**Created:** [December 5, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/filing-out-irb-prototyping-sessions/78436 "2006-12-05T21:16:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Griff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f19dbf/32.png) [@Griff](https://www.ruby-forum.com/u/Griff)\
**Post date:** [December 5, 2006, 9:16pm UTC](https://www.ruby-forum.com/t/filing-out-irb-prototyping-sessions/78436/1 "2006-12-05T21:16:36Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Griff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f19dbf/32.png) [@Griff](https://www.ruby-forum.com/u/Griff)\
**Post date:** [December 5, 2006, 9:24pm UTC](https://www.ruby-forum.com/t/filing-out-irb-prototyping-sessions/78436/2 "2006-12-05T21:24:37Z")

</div>

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-](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/](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

---

<div class="post-metadata">

**Author:** ![Griff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/f19dbf/32.png) [@Griff](https://www.ruby-forum.com/u/Griff)\
**Post date:** [December 5, 2006, 9:28pm UTC](https://www.ruby-forum.com/t/filing-out-irb-prototyping-sessions/78436/3 "2006-12-05T21:28:38Z")

</div>

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. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)

I LIT YOUR GEM ON FIRE!
