views:

58

answers:

1

Hello,

I was curious if anyone had insight on what is the best way for an object to load data from a file in Ruby. Is there a convention? There are two ways I can think of accomplishing this:

  1. Have the initialize method accept a path or file and parse the data within the initialize method, setting the object variables as well.
  2. Have the main "runner" code open the file and parse it, then pass the correct arguments to your constructor.

I am also aware that I could support both methods through an options hash or *args and looking at its size, but I do not have any need to implement both.

+4  A: 

I would use the second option combined with providing the path info as an argument to the main code. This makes it more portable and keeps the object de-coupled from the source of the data

ennuikiller
This makes perfect sense, thank you.
Evan
you're welcome!
ennuikiller