Specifically in ruby, I’m wondering if I am able to do something.
Let’s say I have a while loop that does something until a condition is no longer met.
I will be needing this same while loop for multiple purposes but the condition changes every time. Thus I want change it to a function for re-usability purposes.
Is it possible for me to pass (what condition I’m checking for) as an argument?
Something like:
def wait(condition)
while condition
do something
end
end
To clarify, I want to pass the condition (or statement) itself, not the evaluation of the statement at the time of running the function.
Also I’m pretty sure I’m giving these the wrong names (statement/condition/evaluation) so please correct me if I’m mistaken.
You could do that with blocks:
See http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ for some useful information.