I am aware of just one way to create instance specific methods in ruby i.e.
class Test;end
obj1 = Test.new
class << obj1
def greet
p 'Welcome'
end
end
obj1.greet # "Welcome"
obj2 = Test.new
obj2.greet # through error as,
Traceback (most recent call last):
NoMethodError (undefined method `greet' for #Test:0x0000564fb35acef0>)
or
class Test;end
class << (obj1 = Test.new)
def greet
p 'Welcome'
end
end
obj1.greet # "Welcome"
obj2 = Test.new
obj2.greet # through error as,
Traceback (most recent call last):
NoMethodError (undefined method `greet' for #Test:0x0000564fb35acef0>)
Here I have two questions:
- What is the real world use of such kind of object specific methods?
- What are other different ways to create object specific methods in Ruby?
Classclass) in order to limit their scope to that specific object.