Matthew Bass
Posts: 468
Nickname: pelargir
Registered: Dec, 2005
|
Jared Richardson and Matthew Bass are independent software consultants
|
|
|
|
Building arrays of similar objects in Ruby
|
Posted: Mar 15, 2008 4:41 PM
|
|
|
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.
|
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Matthew Bass
Latest Posts From Pelargir
|
|
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 [...]
Read: Building arrays of similar objects in Ruby
|
|