0

Coming from C# world I am used to thinking classes are immutable definitions of objects and that every object has fixed class.

I am trying to open my mind to possibilities of using

class << some_object
  def something_unique_to_this_object
    # code
  end
end

I am not talking about class << self.
I am talking about changing one or several object's class definition, but not all of them like class << self does.

In several months or almost a year of using ruby I have never found a situation when I thought oh good I can open this objects eigenclass and change it and leave majority of other objects of same class unchanged. Please provide real world examples when you used this.

Marko Avlijaš
  • 1,579
  • 13
  • 27
  • It seems stackoverflow's algorythm thinks this is subjective question. It's not. There are tons of open questions like this: http://stackoverflow.com/questions/1605774/real-world-use-of-binding-objects-in-ruby or http://stackoverflow.com/questions/1343619/php-real-world-oop-example – Marko Avlijaš Feb 14 '17 at 12:56
  • Thank you for finding those old questions that are no longer on-topic and should be closed and deleted! – Jörg W Mittag Feb 14 '17 at 15:38
  • There is a lot more. Why do you think they are not on-topic? – Marko Avlijaš Feb 14 '17 at 15:58
  • List questions are off-topic. Resource requests ("find me an example") are off-topic. Lists-of-examples questions are doubly off-topic. Those questions were *barely* on-topic 8 years ago, when there were only a handful of questions and a handful of users, but they haven't been for a looooong time. – Jörg W Mittag Feb 14 '17 at 16:05
  • Ok but is this question on topic? How do use a pretty exotic feature of a language that most languages do not have? I think it's a great question. It could change the way I use ruby. – Marko Avlijaš Feb 14 '17 at 16:06

1 Answers1

2

You say "not like class << self". Well, guess what - class/module methods are implemented exactly this way. There is a reason for the similarity in syntax. self inside a class definition refers to the class itself. Any class/module methods that you define are actually methods of the eigenclass of that class/module. Your specific class is just one instance of the class Class.


For other examples, look at something like rspec. How would you implement a double and add some methods to it dynamically? How would you stub a method of an existing object? Eingenclasses are an easy and perfect fit for it.


Other than more meta uses, I also sometimes find it comfortable while debugging. Like I can put a breakpoint, alter the behaviour of some object and continue after the breakpoint to see what happens. You might not want to affect all objects of that class or the object might be an instance of an anonymous class.

ndnenkov
  • 35,425
  • 9
  • 72
  • 104
  • First paragraph - I know all that stuff, specifically asked not to use that. :) RSpec is good example. I wish debugger example was less abstract, more concrete, it would open my mind more. Upvoting this answer, hoping for more. – Marko Avlijaš Feb 14 '17 at 13:30
  • @MarkoAvlijaš, I do not remember the exact debug use case. It had something to do with simple_form and an instance of an anonymous class, which was a subclass of another anonymous class (chained a few times). – ndnenkov Feb 14 '17 at 13:57