[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

"Self" Reference

Nathan O.

7/13/2006 10:50:00 PM

Is it possible to define new classes that behave like standard datatypes
when an instance is referred to? For example:

class ListOfBuddies
def initialize
self = []
self.push(Guy.new(name))
end
end
buddy_list = ListOfBuddies.new
4 Answers

Marcin Mielzynski

7/13/2006 10:56:00 PM

0

Nathan Olberding wrote:
> Is it possible to define new classes that behave like standard datatypes
> when an instance is referred to? For example:
>
> class ListOfBuddies
> def initialize
> self = []
> self.push(Guy.new(name))
> end
> end
> buddy_list = ListOfBuddies.new

Then, definitely, inheritance is the right choice:

class ListOfBuddies < Array
def initialize
self.push(Guy.new(name))
end
end

buddy_list = ListOfBuddies.new


lopex

Trans

7/14/2006 1:29:00 AM

0


Nathan Olberding wrote:
> Is it possible to define new classes that behave like standard datatypes
> when an instance is referred to? For example:
>
> class ListOfBuddies
> def initialize
> self = []
> self.push(Guy.new(name))
> end
> end
> buddy_list = ListOfBuddies.new
> .
> .
> .
> buddy_list[0].type == Guy
>
> Is this possible?

My word, the possibilites if you could! You could change an object's
"class" on the fly, hence easy implemention "roles". I think of this as
the "soul" of an object. What are other implications of soul shifting?

T.


Logan Capaldo

7/14/2006 3:46:00 AM

0


On Jul 13, 2006, at 7:00 PM, Marcin Mielzynski wrote:

> Nathan Olberding wrote:
>> Is it possible to define new classes that behave like standard
>> datatypes when an instance is referred to? For example:
>> class ListOfBuddies
>> def initialize
>> self = []
>> self.push(Guy.new(name))
>> end
>> end
>> buddy_list = ListOfBuddies.new
>
> Then, definitely, inheritance is the right choice:
>
> class ListOfBuddies < Array
> def initialize
> self.push(Guy.new(name))
> end
> end
>
> buddy_list = ListOfBuddies.new
>
>
> lopex
>


Ehhh, I wouldn't necessarily choose inheritance. It depends on what
he's doing but ListOfBuddies doesn't strike as having an isa
relationship with an Array. I personally would choose delegation:

Couple of ways to this:

require 'delegate'
class ListOfBuddies < DelegateClass(Array)
def initialize
__setobj__([])
push Guy.new(name)
end
end

or

require 'forwardable'
class ListOfBuddies
extend Forwardable

def initialize
@list = []
push Guy.new(name)
end

def_delegators :@list, :push, :[]
end

The last is my personal favorite. You get to pick exactly what
methods get delegated to the delegate and unlike inheritance you
don't accidently pull in stuff that could possibly kill your
invariants. (esp. with core classes like Array, they have a _lot_ of
methods).



Kick RSS ass

6/20/2009 3:29:00 PM

0

It took 500 cops 50 hrs to get 1 bandit

19 Jun 2009, 1119 hrs IST, Pervez Iqbal Siddiqui & Kapil Dixit, TNN

LUCKNOW/ALLAHABAD: It took around 500 heavily armed elite Uttar Pradesh
cops and commandos more than 50 hours ? apart from four of their own
dead including a PAC commanding officer ? to kill the dreaded dacoit,
Ghanshyam Kevat, in a Chitrakoot village on Thursday afternoon.

After holding on to his fortified position in a two-storey pucca house
for three days, Kevat was finally shot as he tried to escape towards a
nearby forest, after killing a constable, his fourth victim. Alarmingly,
STF chief Brij Lal was leading the team into the house and he could have
as easily been the dacoit's next target. However, in Lucknow, DGP Vikram
Singh crowed about the operation, saying despite 1,500 rounds being
fired and several grenades lobbed, "no civilian casualty or injury was
reported".

The joint team of special task force (STF), special operations group
(SOG) and PAC battalions surrounded the village of Jamauli to nail the
40-plus brigand who has nine cases of cases of murder, extortion and
dacoity against him.

The operation was the lengthiest in the history of UP police. Apart from
surprise attacks by Naxals or the recent ambush by slain brigand Thokia
and his gang in which nine STF men were killed, the Jamauli encounter is
perhaps the only incident in which four cops were killed while 11 others
including an IG and DIG were injured battling a single bandit.

All that Kevat had was .315 bore rifle and some bullets. With more
sophisticated weapons and ammunition, it's anybody's guess how much
longer he could have held out against UP's crack units.

http://timesofindia.indiatimes.com/articleshow/4...

It's no surprise that the incompetent UP police then resort to this:
http://timesofindia.indiatimes.com/Cities/Lucknow/GRP-arrests-constables-for-throwing-woman-child-off-train/articleshow/4...


--
THE NAZI-STYLE HATE-MINORITIES MESSAGE OF THE `RSS BIBLE'

"The foreign races in Hindusthan must either adopt the Hindu culture
and language, must learn to respect and hold in reverence Hindu
religion, must entertain no idea but those of the glorification of the
Hindu race and culture, i.e., of the Hindu nation and must loose their
separate existence to merge in the Hindu race, or may stay in the
country, wholly subordinated to the Hindu Nation, claiming nothing,
deserving no privileges, far less any preferential treatment -- not
even citizen's rights. There is, at least, should be, no other course
for them to adopt. We are an old nation; let us deal, as old nations
ought to and do deal, with the foreign races, who have chosen to live
in our country".

-- Madhav Sadashiv Golwalkar's, We or Our Nationhood Defined