From: Matt Mencel Date: 2012-05-16T23:25:22+09:00 Subject: Passing attributes to initialize Hi, I'm not an OOP person. Most of the Ruby I write is just scripts and such. I've found a good place to start using real Class objects though and am having a hard time getting my head around it. I have this class. The @members attribute is an array of several 5 digit numbers (could be between 1 and 10 values in the array). I want to be able to find objects in the Class by using one of the numbers. class Crosslisting attr_accessor :members, :title, :id def initialize(members, name='', id='') @members = members @name = name @id = id end def self.find_by_member(member) found = nil ObjectSpace.each_object(Crosslisting) { |o| found = o if o.members.include?(member) } found end end members = ['12345','67890'] my_obj = Crosslisting.new(members) ap Crosslisting.find_by_member('12345') # OUTPUT USING AWESOME_PRINT GEM # This seems to work ok. Am I doing it right or is there a better way? I tried to figure out how to use 'def initialize(*args)' instead of naming all the attributes, but I'm not currently smart enough to make it work apparently. I thought about trying to use Hashes and symbols...maybe that's a better way or maybe not....but again haven't figured out how to make it work....something like... my_obj = Crosslisting.new(:member => members) Thanks, Matt