Well I don't know Ruby and don't understand the exact example given, but I suspect it would be something like:
dataSource.Update(id, person => person.PhoneNumber = phoneNumber);
Where DataSource.Update
would:
- Have a signature of something like
void Update(string id, Action<Person> updateAction
(or possibly return abool
to indicate whether or not it found the person) - Be implemented as:
- Find the person with the given ID
- If it doesn't exist, return immediately
- Otherwise, execute the given action, and update the backing store with the modified object
Or more generally (and closer to the original Ruby):
dataSource.WithPerson(id, person => {
person.PhoneNumber = phoneNumber;
dataSource.UpdatePerson(person);
};
Personally I prefer the first form: it's more specific about what it's trying to achieve, but that may well lend itself to a better implementation, and it's certainly cleaner in the calling code.