# From irb to .rb? and general question

**URL:** <https://www.ruby-forum.com/t/from-irb-to-rb-and-general-question/111601>\
**Category:** Ruby\
**Created:** [August 9, 2007, 12:41am UTC](https://www.ruby-forum.com/t/from-irb-to-rb-and-general-question/111601 "2007-08-09T00:41:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [August 9, 2007, 12:41am UTC](https://www.ruby-forum.com/t/from-irb-to-rb-and-general-question/111601/1 "2007-08-09T00:41:36Z")

</div>

say I’ve been mucking about in irb and want to save some of/all  
previous steps into file.rb, is there a way to do this?

also, I’ve been going through (mostly reading) some tutorials, and am  
just slightly confused on how it works when you call up something, a  
class or a method definition, and then recall it later… it doesn’t  
overwrite what’s previously there, just add to it? though in some  
cases it seems to overwrite what’s already there… but this clearly  
isn’t the case if I’m making methods in the Array class, for example.

---

<div class="post-metadata">

**Author:** ![simonsaid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b5e925/32.png) [@simonsaid](https://www.ruby-forum.com/u/simonsaid)\
**Post date:** [August 9, 2007, 12:55am UTC](https://www.ruby-forum.com/t/from-irb-to-rb-and-general-question/111601/2 "2007-08-09T00:55:36Z")

</div>

On Thu, Aug 09, 2007, Simon S. wrote:

> say I’ve been mucking about in irb and want to save some of/all  
> previous steps into file.rb, is there a way to do this?

My irb history hack can do this:

[http://blog.bleything.net/pages/irb\_history](http://blog.bleything.net/pages/irb_history)

history\_write (aliased to hw) takes a filename and a collection of lines  
and writes those lines to the file.

Alternately, if you’ve enabled saving history across sessions, you can  
just extract the lines you want from the file it uses for this. Try  
~/.irb.history or ~/.irb\_history, though I’m sure there are other  
variants.

> also, I’ve been going through (mostly reading) some tutorials, and am  
> just slightly confused on how it works when you call up something, a  
> class or a method definition, and then recall it later… it doesn’t  
> overwrite what’s previously there, just add to it? though in some  
> cases it seems to overwrite what’s already there… but this clearly  
> isn’t the case if I’m making methods in the Array class, for example.

Sorta! When you re-open a class, you’re adding. If you “re-open” a  
method, you’re overriding.

```
str = "string"

str.reverse
=> "gnirts"

class String
  # additive
  def upper_case_it
    self.upcase
  end

  # replace...ative
  def reverse
    return "reversed!"
  end
end

str.upper_case_it
=> STRING

str.reverse
=> "reversed!"

```

Ben
