eonil
eonil

Reputation: 86165

How to add/modify/delete a method in Pry?

I am trying Pry for main code REPL editor.

I discovered this is very close to what I was looking for, but I don't know well about how to use it. I want to know how to add/modify/remove each method (or any other members) to/from a class.

When I tried this,

➜  ~  pry
[1] pry(main)> class AAA
[1] pry(main)*   def bbb
[1] pry(main)*     "ccc"
[1] pry(main)*   end  
[1] pry(main)* end  
=> nil
[2] pry(main)> cd AAA
[3] pry(AAA):1> ls
AAA#methods: bbb
locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_
[4] pry(AAA):1> def xxx
[4] pry(AAA):1*   "yyy"
[4] pry(AAA):1* end  
=> nil
[5] pry(AAA):1> def xxx
[5] pry(AAA):1*   "zzz"
[5] pry(AAA):1* end  
=> nil
[6] pry(AAA):1> cd ..
[7] pry(main)> Pry.WrappedModule(AAA).source
=> "class AAA\n  def bbb\n    \"ccc\"\n  end\nend\ndef xxx\n  \"yyy\"\nend\ndef xxx\n  \"zzz\"\nend\n"
[8] pry(main)> AAA.new.xxx
=> "zzz"
[9] pry(main)> 

It worked well as I expected. But The source code contains duplicated definition of xxx method. If I want to erase older one (or both), how can I do that? Also, if I want to remove existing method (or any other member) without exchanging to a new one, how can I do that?

P.S. I am doing this mainly to edit, store and restore class source code between memory and disk. (a kind of image based persistent)

Upvotes: 0

Views: 467

Answers (1)

eonil
eonil

Reputation: 86165

I found the answer. Use undef command.

[11] pry(main)> cd AAA
[13] pry(AAA):1> ls
AAA#methods: bbb  xxx
locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_
[14] pry(AAA):1> undef xxx
=> nil
[15] pry(AAA):1> ls
AAA#methods: bbb
locals: _  __  _dir_  _ex_  _file_  _in_  _out_  _pry_
[16] pry(AAA):1> cd ..
[17] pry(main)> Pry.WrappedModule(AAA).source
=> "class AAA\n  def bbb\n    \"ccc\"\n  end\nend\n"
[18] pry(main)> 

Upvotes: 1

Related Questions