-2

I'm unable to find a method that allows in place modification of object with arbitrary methods object implements that return a value.

What I'm hoping to do is

class MyClass
  def return_modified_my_class_instance
    ...
  end
end

object = MyClass.new
object.in_place!(&:return_modified_my_class_instance)

but I'm not interested in implementing this myself. I only want to do this if a in_place! equivalent exists on Object in ruby

Matthias Michael Engh
  • 1,159
  • 2
  • 10
  • 25
  • 1
    Can you clarify this a little? What exactly is `object.in_place!(&:return_modified_my_class_instance)` supposed to do? Is this supposed to replace `object` with `object.return_modified_my_class_instance`? Are you looking for something like [`Object#tap`](http://ruby-doc.org/core-2.5.0/Object.html#method-i-tap)? Perhaps a concrete example would be helpful. – mu is too short Apr 27 '18 at 22:41
  • 1
    Thank you for your question! I'm happy to try to clarify. There are some instance methods that return an new instance of same class (for example ActiveRecord scope methods) I would like to call that method, like in send, however I want that method to modify the object the method is being called on – Matthias Michael Engh Apr 27 '18 at 23:28
  • I can do the same thing by: `whatever = whatever.modified_instance_of_same_class` – Matthias Michael Engh Apr 27 '18 at 23:31
  • but it would be additionally nice if it ensured it keeps the current ancestors – Matthias Michael Engh Apr 27 '18 at 23:32
  • `Object#tap`, if there was a `tap!` variant that modifies the current instance to whatever return value of the last statement within tap – Matthias Michael Engh Apr 27 '18 at 23:35
  • What Ruby version are you using?, can you add a "reproducible" example and expected output? – Sebastián Palma Apr 27 '18 at 23:44
  • You want a method that acts like assignment? I'm not aware of any sane way to do such a thing. This sounds like an [XY Problem](http://xyproblem.info) to me. Do you have some specific examples of what you're trying to do that makes you think you need your proposed solution? ActiveRecord scopes don't work like that, they just give you an object that they've copied a bunch of methods into; STI constructors sort of pretend to act something like that but that's just an illusion. – mu is too short Apr 28 '18 at 00:55
  • Can you check if your question is related to [this question](https://stackoverflow.com/questions/803020/redefining-a-single-ruby-method-on-a-single-instance-with-a-lambda)? – André Guimarães Sakata Apr 28 '18 at 01:36

1 Answers1

0

This is impossible. A method that modifies any object can only know how to manipulate the state which is common to all objects, and that is none.

A method that can replace any object in Ruby with a new one is impossible since object references cannot change their class in Ruby.

It sounds like you are a Smalltalk programmer, and you are looking for an equivalent to become:? That is impossible in Ruby.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653