This post originated from an RSS feed registered with Ruby Buzz
by Matthew Bass.
Original Post: Building arrays of similar objects in Ruby
Feed Title: Pelargir
Feed URL: http://feeds.feedburner.com/pelargir/
Feed Description: Musings on software and life from Matthew Bass. Regular posts on new web products, tips and tricks, etc.
I often find myself having to build arrays of similar objects in my tests. For example:
def create_user
:some_user
end
users = [create_user, create_user, create_user]
It seems wasteful to repeat the same method call three times. One solution is to use the #times method to append to an array, like this:
users = []
3.times { users << create_user }
This just doesn’t [...]