-1

In my code, I have an array of ActiveRecord::Base objects, and to save them, for example, I would do something like this:

obj_list = [ar_obj1, ar_obj2, ar_obj3]
obj_list.each { |obj| obj.save! }

It would perhaps be nice and DRY to say instead:

obj_list.each.save!

However, I cannot see how to do this.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114

1 Answers1

2

You can use Symbol#to_proc shortcut here. But it's still a block, only hidden.

obj_list.each(&:save!)
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • Thanks - that is just what I was looking for! I think it is more readable in that form for casual readers. – Ken Y-N Oct 02 '13 at 06:44