bst
bst

Reputation: 437

ruby `pry`: can I edit classes defined on the REPL?

Context: exploratory experimenting written directly in the REPL.

In pry, if I type edit fn_name I can open the definition of fn_name in an editor and change it. Say:

def fn_name
  puts 'fn_name'
end

I can also edit class methods if I type cd ClassName and edit method_name, say:

def method_name
  puts 'method_name'
end

But if I type edit ClassName, it fails with

Error: Cannot find a file for ClassName!

Is there a way to open the whole definition for a class? Say, something like:

class ClassName
  def method_name
    puts 'method_name'
  end

  def self.class_method_name
    puts 'class_method_name'
  end

  private

  def private_method_name
    puts 'private_method_name'
  end
end

Update: I'm not asking for pry to give me code listing for arbitrary classes from the standard library or otherwise. I'm specifically interested in editing classes that I have typed on the REPL.

Update 2: I see many comments being written in a defensive way, as if I'm criticizing the tool or claiming it should be this or that way. To be clear: I'm brand new to it and I had no idea how it works internally. I have no criticism of a tool I don't quite understand yet. I'm just trying to find my way around it.

All I want is to know whether something is possible or not, because I couldn't figure that out from the documentation. Some comments were very helpful and gave me some understanding of how the tool works.

So far my understanding is that what I'm looking for is not quite possible, but there are workarounds that could be good enough for me.

Also, to be clear: "That's not possible" is a totally fine answer. Even better if it comes with "but here's the next best workaround".

Upvotes: 0

Views: 76

Answers (0)

Related Questions