More than one initializer

J

Jp Hastings-spital

Hi all,

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, :tweet
def initialize(text)
@tweet = text
# Post to twitter
@id = id_from_post_operation
end
end

class Search
def self.do(q)
# perform search
out = []
results.each do |hit|
# This is the problem point:
out << Item.new_from_data(hit.id,hit.text)
end
return out
end
end
end

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, but if I
use the standard MyTwitter::Item.new I'll be posting a new item,
not simply creating an object with the id I have from my search.

Is there a way to create an object with a given attribute without
calling .new?

I hope I've explained myself well enough! Thanks,
JP
 
R

Robert Klemme

Hi all,

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, :tweet
def initialize(text)
@tweet = text
# Post to twitter
@id = id_from_post_operation
end
end

class Search
def self.do(q)
# perform search
out = []
results.each do |hit|
# This is the problem point:
out << Item.new_from_data(hit.id,hit.text)
end
return out
end
end
end

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, but if I
use the standard MyTwitter::Item.new I'll be posting a new item,
not simply creating an object with the id I have from my search.

Is there a way to create an object with a given attribute without
calling .new?

I hope I've explained myself well enough! Thanks,
JP

You could do:

class Item
def self.new_from_data(id, text)
it = allocate
it.id = id
it.text = text
it
end
end

Or change your initialize logic to handle both cases.

Cheers

robert
 
E

Ehsanul Hoque

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=
 
J

Jp Hastings-spital

Cheers Ehsanul, yeah, they're not tweets - just an easy way to make the
situation understood! But thanks for your help!

Robert - thanks for the 'allocate' tip, that will probably be the way
forward here. However 'id' and 'tweet' are read-only attributes (and I'd
like them to remain that way):

NoMethodError: undefined method `id=' for #<MyTwitter::Item:0x1019bad50>

I think I might play with altering the logic inside .new to
transparently accept id/text as well, seems like the simplest way
forward.

Cheers!
JP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top