Is there a default difference between methods with '!' and without, in ruby?
collect v collect!
flatten v flatten!
and so on..
Is there a default difference between methods with '!' and without, in ruby?
collect v collect!
flatten v flatten!
and so on..
In ruby the main difference is that, the !
methods are selfish, i.e. they apply the changes to the self
object. They return nil
, when no changes are done, while the non-!
methods create new modified object.
In Rails the difference is that, the !
methods are safe versions of non-!
methods, that means the !
methods raises an exception when the code encountered an error during execution, while non-!
methods just return error state, usually false
condition.