From: Ehsanul Hoque Date: 2009-09-28T00:17:30+09:00 Subject: Re: More than one initializer --_2e55dba5-cae4-4512-a090-d09d0bb306b0_ Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable Hi > Hi all=2C >=20 > I have a module containing two classes the first is 'item' the second is > 'search'. Lets use twitter as an example: > module MyTwitter > class Item > attribute_reader :id=2C :tweet > def initialize(text) > @tweet =3D text > # Post to twitter > @id =3D id_from_post_operation > end > end >=20 > class Search > def self.do(q) > # perform search > out =3D [] > results.each do |hit| > # This is the problem point: > out << Item.new_from_data(hit.id=2Chit.text) > end > return out > end > end > end >=20 > When I create a new Item object I have code that creates a new tweet > however I'd like my Search to return an array of Item objects=2C but if I > use the standard MyTwitter::Item.new I'll be posting a new item=2C > not simply creating an object with the id I have from my search. >=20 > Is there a way to create an object with a given attribute without > calling .new? You're probably better off by not making Item.new post a tweet. Instead=2C = have your initialize method only create an object with id/text attributes s= et if they are passed in. Then=2C create a self.post method to actually pos= t tweets. It could look like this: class Item attribute_reader :id=2C :tweet def initialize(id=2C text) @id=2C @tweet =3D id=2C text end def self.post(text) # Post to twitter self.new(id_from_post_operation=2C text) end That'll solve it I hope. By the way=2C if your items are tweets=2C it makes= more sense to call the class Tweet rather than Item. And then the @tweet a= ttribute could be @text=2C to make it clear as to what it is. the Search cl= ass probably should just be incorporated into the Tweet class=2C rather tha= n in a separate class=2C unless you're using search across a number of thin= gs and not just tweets. But if your items aren't tweets=2C well I'm just be= ing silly :) =0A= _________________________________________________________________=0A= Lauren found her dream laptop. Find the PC that=92s right for you.=0A= http://www.microsoft.com/windows/choosepc/?ocid=3Dftp_val_wl_290= --_2e55dba5-cae4-4512-a090-d09d0bb306b0_--