I wanted to use value.respond_to?(:dup) ? value.dup : value in order to check if I can duplicate an object but it failed with TypeError on booleans, nil or such "primitives" alike.
I've ended up with:
begin
value = value.dup
rescue
#ignore, use the original if no dup-able (e.g nil, true, etc)
end
Is there a better way?
Bonus: Why does it responds :dup?
Not deep dup, just for the question.
EDIT: Thoughts:
obj.class.methods.include? :new is nice but a bit too hackish i think it has bad performance Marshal also looks like an overkilldup being defined at the object level is wrong.So, quoting @Linuxios
There isn't really a better way
You can write it as one line this way:
value = value.dup rescue value
Very clear.
It is standard to define a dup method that raises TypeError for types that cannot be duplicated. Thus any object will "respond to" it. You really have to call it and check with a begin-rescue-end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With