On Jan 22, 2007, at 5:48 AM, Helder Ribeiro wrote:

> Yes, but sometimes you can't chop it up so fine-grained that you have
> self-contained, one-lined commands (that's what I meant by
> 'scripting').

I think it's harder for some projects than others, but I'm not  
willing to go all the way to "can't".  ;)

> Say you write up a method and want to correct it, you have retype or
> find through the history by pressing "Up" all the lines that "worked"
> and re-entering them. You also can't just read them from a file  
> either,
> AFAIK (not smthg like an incomplete method code, for instance).
>
> Conversely, it's not trivial to go through the lines that "worked" in
> that awkward "Up"-key fashion, then select them and save them out to a
> file.

Here are some ideas:

 >> def test
 >>   puts "This line is OK."
 >>   puts "So is this one."
 >>   Object.new.no_such_method  # Oops!
 >> end
=> nil
 >> test
This line is OK.
So is this one.
NoMethodError: undefined method `no_such_method' for #<Object:0x102f9ac>
         from (irb):4:in `test'
         from (irb):6
 >> h 6
[0758] def test
[0759]   puts "This line is OK."
[0760]   puts "So is this one."
[0761]   Object.new.no_such_method  # Oops!
[0762] end
[0763] test
[0764] h 6
=> nil
 >> get_lines(758..760) << "puts 'Now this is better.'" << "end"
=> ["def test", "  puts "This line is OK."", "  puts "So is this  
one."", "puts 'Now this is better.'", "end"]
 >> eval((get_lines(758..760) << "puts 'Now this is better.'" <<  
"end").join("\n"))
=> nil
 >> test
This line is OK.
So is this one.
Now this is better.
=> nil

You can also write to a file:

 >> hw "test_method.rb", 758..760
=> nil
 >> File.read("test_method.rb")
=> "def test\n  puts "This line is OK."\n  puts "So is this one."\n"
 >> File.open("test_method.rb", "a") { |f| f << "  puts 'Fixed in the  
file.'\nend\n" }
=> #<File:test_method.rb (closed)>
 >> File.read("test_method.rb")=> "def test\n  puts "This line is  
OK."\n  puts "So is this one."\n  puts 'Fixed in the file.'\nend\n"
 >> load "test_method.rb"
=> true
 >> test
This line is OK.
So is this one.
Fixed in the file.
=> nil

The methods I am using here come from:

http://blog.bleything.net/pages/irb_history

Hope that helps.

James Edward Gray II