Is there a method that tells if an object is mutable, similar to mutable? in the following? If not, what is the best way to implement it?
"abcde".mutable? # => true
0.mutable? # => false
To answer mu is too short and dbenhur’s question, I do not like the syntax of enumerated.inject(initial){...} or enumerated.each_with_object(initial){...}. I wanted a method that reverses the receiver and the argument, and I wanted it to be available to a wide variety of classes; so that I have:
initial.my_new_method(enumerated){...}
0.my_new_method(1..10){|sum, i| sum + i} # => 55
"a".my_new_method(b: 3, c: 4){|s, (k, v)| s + k.to_s * v} # => "abbbcccc"
This will make the return a modified version of the receiver, and is conceptually more natural. And with my_new_method, I wanted it to be non destructive. When the receiver is mutable, I further wanted to define a destructive version
initial.my_new_method!(enumerated){...}
"a".my_new_method!(b: 3, c: 4){|s, (k, v)| s << k.to_s * v} # => "abbbcccc"
So to detect whether the receiver is mutable or not is necessary. It does not matter if it is frozen. If I use the destructive version of the method with a frozen object, it will simply raise an error. Nothing wrong with that.
I came up with this