Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > More than one initializer

Reply
Thread Tools

More than one initializer

 
 
Jp Hastings-spital
Guest
Posts: n/a
 
      09-27-2009
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      09-27-2009
On 27.09.2009 15:07, Jp Hastings-spital wrote:
> 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


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
Reply With Quote
 
 
 
 
Ehsanul Hoque
Guest
Posts: n/a
 
      09-27-2009

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/cho...tp_val_wl_290=

 
Reply With Quote
 
Jp Hastings-spital
Guest
Posts: n/a
 
      09-27-2009
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
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can one declare more than one signal on one line? Merciadri Luca VHDL 4 11-01-2010 02:00 PM
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com Java 0 03-06-2008 08:11 AM
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com C++ 0 03-05-2008 08:41 AM
Like all great travelers, I have seen more than I remember andremember more than I have seen. shenrilaa@gmail.com C Programming 0 03-05-2008 03:26 AM
composition/aggregation: would like to use constructor body rather than initializer list Chris K C++ 1 04-17-2004 08:38 PM



Advertisments