With strings one can do this:
a = "hello"
a.upcase!
p a #=> "HELLO"
But how would I write my own method like that?
Something like (although that doesn’t work obviously):
class MyClass
def positify!
self = [0, self].max
end
end
I know there are some tricks one can use on String but what if I’m trying to do something like this for Object?
Well, the
upcase!method doesn’t change the object identity, it only changes its internal structure (s.object_id == s.upcase!.object_id).On the other hand, numbers are immutable objects and therefore, you can’t change their value without changing their identity. AFAIK, there’s no way for an object to self-change its identity, but, of course, you may implement
positify!method that changes properties of its object – and this would be an analogue of what upcase! does for strings.