I’m new to ruby and feel that I still do a lot of things in C sharpish way :-). Suppose you have an array of objects (Question :has_many=>:answers). I want to iterate over all answers and if some answers meet a criteria, change answer attribute. Currently I’m doing it as follows:
def iterate_and_do_stuff for (a in self.answers) if(a.somecriteria==true) a.some_attr=some_val end end end
What are other ways of doing this? Blocks, loops, etc?
Please adivce.
Thank you.
Use Array#each:
self.answers.each {|a| a.some_attr = some_val if a.some_criteria}