Suppose a certain part of code needs to be run inside a different block/context, depending on a configuration option. For example,
if silence
silence_stdout do
# do something
end
else
# do the same thing
end
Is there a way to write this without repeating the # do something code?
Depending on how much code “do something” is you could throw it in a lambda:
or throw it in a method:
You could also turn the logic inside out a bit:
Or you could use whole classes or just lambdas instead of methods in that last one.