It would be cool to have mv and cp as commands #664

Closed
TreyLawrence opened this Issue Jul 23, 2012 · 5 comments

Projects

None yet

4 participants

Contributor

To move and copy locals to other bindings.

Owner

Interesting, can you give an example of how this would work?

Owner
kyrylo commented Jul 24, 2012

Probably something like this:

pry(main)> @foo = 10
pry(main)> cd 1
pry(main):1> cp ../@foo ./
pry(main):1> ls -i
instance variables: @foo
pry(main):1> cd
pry(main)> ls -i
instance variables: @foo

If so, to tell the truth, I don't see a use of this.

Owner
kyrylo commented Jul 28, 2012

@TreyLawrence, explain what you meant, please.

Contributor

@kyrylo, You pretty much explained it already. I thought it would be a cool feature, and I've already run into a few situations where I needed to switch scope, and thus abandon any locals that I've created. This can be frustrating, especially when it's a local that takes many lines to initialize.

Owner

I think this should not be in pry itself. You can add the "cp" command to your ~/.pryrc with:

Pry::Commands.create_command "cp" do
  description "Move a variable from a different scope into this one"

  banner <<-BANNER
    Usage: cp ../f

    Copies f from the frame above to this one.
  BANNER

  def process
    binding_path, var_name = arg_string.split(%r{/(?=[^/]*)\z})
    stack, _ = context_from_object_path(binding_path, _pry_, state.old_stack)

    Thread[:pry_cd_tmp] = stack.last.eval(var_name)
    target.last.eval("#{var_name} = Thread[:pry_cd_tmp]")
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment