Question

I am practising with IRB (actually Wirble). I was wondering if any hacking inside IRB could be dumped into a file for later diff-ing, editing and reusing?

You create classes, methods, data on the go, and the interactive session from time to time contains valuable milestones as you evolve with your code - so this would be valuable to reuse in source form.

EDIT: To further clarify the original question: I would like to do this in IRB:

class A; def m1; end; end
class A; def m2; end; end

and then at the end dump class A as

class A

  def m1
  end

  def m2
  end

end

(not necessarily pretty-printing the code :-) )

EDIT: Re: @DGM/pry: "pry" seems to be cool, but it's introspection seems to be somewhat buggy, and it does not do what is expected. I did what is above, and the m1 method is not listed if I used show-method A :

[1] pry(main)> class A; def m1; end; end     
=> nil

[2] pry(main)> class A; def m2; end; end
=> nil

[3] pry(main)> show-method A#m1
[...]
class A; def m1; end; end

[4] pry(main)> show-method A#m2
[...]
class A; def m2; end; end

[5] pry(main)> show-method A
[...]
class A; def m2; end; end

EDIT: I have filed a bug report, and @banister was very helpful with the pry issue, see the the ticket, there is very valuable data there.

EDIT: @banister suggested using edit and it makes sense to follow that workflow.

Was it helpful?

Solution 2

In Pry, if you create the classes using edit rather than inside the REPL it should work fine, see the following showterm:

http://showterm.io/434d8965312292c8830c9#fast

OTHER TIPS

I'm not sure about irb, but if you use the more powerful pry which is designed to be a more powerful replacement for irb, you can do this:

1] pry(main)> class Foo
[1] pry(main)*   def bar
[1] pry(main)*     1
[1] pry(main)*   end
[1] pry(main)* end
=> nil
[2] pry(main)> show-method Foo#bar

From: (pry) @ line 2:
Number of lines: 3
Owner: Foo
Visibility: public

def bar
  1
end
[3] pry(main)> show-method Foo

From: (pry) @ line 1:
Number of lines: 5

class Foo
  def bar
    1
  end
end

There is such a file. Check out ~/.irb-history:

$ irb
1.9.3p429 :001 > 2+2
 => 4
1.9.3p429 :002 > 3+3
 => 6
1.9.3p429 :003 > exit
$ tail -n 3 ~/.irb-history
2+2
3+3
exit

Unfortunately there is no way in ruby to dump the source of a dynamically created class. For example:

>> class Test
>>   value = rand
>>   define_method(:f) do 
?>     value
>>   end
>> end
=> #<Proc:0x007fed0b19b2c0@(irb):3 (lambda)>
>> Test.new.f
=> 0.7602337424749085
>> Test.new.f
=> 0.7602337424749085

There is no possible way to recover the Test class' source. There are various gems that can get the source of a method, but they work by reading the source from the file it appears in, and cannot work with dynamically generated methods.

The only real way you can achieve this then is to just use the IRB history, and extract what you need from there. This file is located at ~/.irb-history. It may be possible to automate this (eg extract definitions from that file from the current IRB sessions), but I don't think anyone has done that yet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top