Add a callback function to a Ruby array to do something when an element is added

后端 未结 3 1312
后悔当初
后悔当初 2021-02-04 22:21

I\'d like to add something like a callback function to a Ruby array, so that when elements are added to that array, this function is called. One thing I can think of is to overr

3条回答
  •  天涯浪人
    2021-02-04 23:18

    Use the "Observer" pattern to be notified of changes in the size of the array you wish to observer: Ruby Observer This saves you from having to override all methods that add an element to the array

提交回复
热议问题