[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: simple subclass question

Ball, Donald A Jr (Library)

5/2/2007 6:57:00 PM

> just initialize @foods in the class
>
> class Breakfast
> @foods = [] # class instance variable, I did this in my
> original example
> # because of (1)
> def self.add_food( *args)
> @foods += args
> end
>
> ......

but then @foods doesn't exist in RubyBreakfast and BoringBreakfast. I
mean, sure, I could cut'n'paste @foods = [] in each of them, but that
doesn't smell right. In the superclass, I want a chunk of code that
initializes a class instance variable in itself and each of its class
descendents.

In point of fact, I can work around the @foods.nil? case in the
self.foods method, but I'd rather figure out either how to do this or
why it's a bad idea to do it. :)

- donald

7 Answers

Pit Capitain

5/2/2007 8:10:00 PM

0

Ball, Donald A Jr (Library) schrieb:
> In point of fact, I can work around the @foods.nil? case in the
> self.foods method, but I'd rather figure out either how to do this or
> why it's a bad idea to do it. :)

Donald, I would add

def self.my_foods
@foods ||= []
end

and then use my_foods instead of @foods in the other two methods. You
could also take a look at Ara's attributes library:

http://codeforpeople.com/lib/ruby/attributes/attributes-3....

Regards,
Pit

Robert Dober

5/2/2007 8:57:00 PM

0

On 5/2/07, Ball, Donald A Jr (Library) <donald.ball@nashville.gov> wrote:
> > just initialize @foods in the class
> >
> > class Breakfast
> > @foods = [] # class instance variable, I did this in my
> > original example
> > # because of (1)
> > def self.add_food( *args)
> > @foods += args
> > end
> >
> > ......
>
> but then @foods doesn't exist in RubyBreakfast and BoringBreakfast. I
> mean, sure, I could cut'n'paste @foods = [] in each of them, but that
> doesn't smell right. In the superclass, I want a chunk of code that
> initializes a class instance variable in itself and each of its class
> descendents.
You mean you want to have your cake *and* eat it ;)?
Sure enough, but there is no simple solution, you could do something like this

class Module
def inherit_cl_inst_vars # refinement via args as you wish
superclass.instance_variables.each do
| ivar |
instance_variable_set ivar,
superclass.instance_variable_get( ivar )
end
end
end

class A
@a = 42
end

class B < A
inherit_cl_inst_vars
end

class << A
attr_accessor :a
end

puts A.a
puts B.a
B.a=1764
puts A.a
puts B.a
>
> In point of fact, I can work around the @foods.nil? case in the
> self.foods method, but I'd rather figure out either how to do this or
> why it's a bad idea to do it. :)
>
> - donald
>
>

Robert
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Gary Wright

5/3/2007 4:44:00 AM

0


On May 2, 2007, at 2:57 PM, Ball, Donald A Jr (Library) wrote:
> In the superclass, I want a chunk of code that
> initializes a class instance variable in itself and each of its class
> descendents.

You can use #inherited to trigger your own class initialization:

class Base
class <<self
def inherited(other)
other.instance_eval { initialize_class }
end
def initialize_class
@foo = 42
end
attr_accessor :foo
end
initialize_class
end

class Subclass < Base
puts "foo is #{@foo}" # 42
end

puts Subclass.foo # 42
puts Class.new(Base).foo # 42



Gary Wright




Robert Dober

5/3/2007 4:57:00 AM

0

On 5/3/07, Gary Wright <gwtmp01@mac.com> wrote:
>
> On May 2, 2007, at 2:57 PM, Ball, Donald A Jr (Library) wrote:
> > In the superclass, I want a chunk of code that
> > initializes a class instance variable in itself and each of its class
> > descendents.
>
> You can use #inherited to trigger your own class initialization:
>
> class Base
> class <<self
> def inherited(other)
> other.instance_eval { initialize_class }
> end
> def initialize_class
> @foo = 42
> end
> attr_accessor :foo
> end
> initialize_class
> end
>
> class Subclass < Base
> puts "foo is #{@foo}" # 42
> end
>
> puts Subclass.foo # 42
> puts Class.new(Base).foo # 42
>
>
>
> Gary Wright

Wow that scales much better than mine Gary, nice.
However maybe you want to inherit @foo dynamically, that is
I suggest this slight adaptation of Base

529/29 > cat inherited.rb && ruby inherited.rb
# vim: sts=2 sw=2 expandtab tw=0 nu:
class Base
@foo = 42
class <<self
def inherited(other)
other.instance_eval { initialize_class }
end
def initialize_class
@foo = superclass.instance_variable_get("@foo")
end
attr_accessor :foo
end
end

class Subclass < Base
puts "Sub foo is #{@foo}" # 42
end
Base.foo = 1764
class Another < Base
puts "Another foo is #{@foo}" # 42
end

puts Subclass.foo # 42
puts Class.new(Base).foo # 42
Sub foo is 42
Another foo is 1764
42
1764

What do you think, anyway the two behaviors might be what you want, I
actually need the second behavior.

Cheers
Robert


--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

Rick DeNatale

5/3/2007 12:10:00 PM

0

On 5/2/07, Robert Dober <robert.dober@gmail.com> wrote:

> You mean you want to have your cake *and* eat it ;)?

Actually, this is no problem at all, in fact you have to have your
cake before you eat it.

The trick is to eat your cake and have it too.

Almost everyone gets this old saying backwards.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Robert Dober

5/4/2007 10:27:00 AM

0

On 5/3/07, Rick DeNatale <rick.denatale@gmail.com> wrote:
> On 5/2/07, Robert Dober <robert.dober@gmail.com> wrote:
>
> > You mean you want to have your cake *and* eat it ;)?
>
> Actually, this is no problem at all, in fact you have to have your
> cake before you eat it.
>
> The trick is to eat your cake and have it too.
>
> Almost everyone gets this old saying backwards.
As the English language is the greatest victim of trivialization by
foreign speakers and writers I appreciate enormously to get these
things right.
It is a great irony that countries like France, where I happen to
live, are very worried about the influence of English - and they are
right because I do not feel that English verbs scale very nicely into
latin languages but this is beyond my point.

The true "victim" of English's globalization is English itself. But
that is how languages have evolved for millennia now and there is
nothing we can do about it. (Imagine the shock Sallust, Cicero or
Caesar would have had when hearing the vulgo from of Latin that gave
birth to latin languages).


But I do not like it nonetheless.

Cheers
Robert

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

John Joyce

5/4/2007 10:56:00 AM

0


On May 4, 2007, at 7:27 PM, Robert Dober wrote:

> On 5/3/07, Rick DeNatale <rick.denatale@gmail.com> wrote:
>> On 5/2/07, Robert Dober <robert.dober@gmail.com> wrote:
>>
>> > You mean you want to have your cake *and* eat it ;)?
>>
>> Actually, this is no problem at all, in fact you have to have your
>> cake before you eat it.
>>
>> The trick is to eat your cake and have it too.
>>
>> Almost everyone gets this old saying backwards.
> As the English language is the greatest victim of trivialization by
> foreign speakers and writers I appreciate enormously to get these
> things right.
> It is a great irony that countries like France, where I happen to
> live, are very worried about the influence of English - and they are
> right because I do not feel that English verbs scale very nicely into
> latin languages but this is beyond my point.
>
> The true "victim" of English's globalization is English itself. But
> that is how languages have evolved for millennia now and there is
> nothing we can do about it. (Imagine the shock Sallust, Cicero or
> Caesar would have had when hearing the vulgo from of Latin that gave
> birth to latin languages).
This is OT.
But I'll bite.
Utter nonsense. Languages are never victims. No human language is
canonical. It isn't possible. English is one of the few languages
whose grammar books are not dictated by committee, but updated to
reflect modern usage. English, like all human languages is flexible
and fuzzy. Now the number of speakers of English as a second language
outnumber native English speakers. This simply means that English now
has more dialects than ever and where they all overlap is where you
will find the language. Language and culture are often difficult to
separate and thus societies attempt to preserve something that is
intangible. Language and culture are organic and depend on each
connection between individuals to identify what is and is not
functioning symbolic code for communication of ideas. Language is its
own disruptive technology. It contradicts, counteracts and alters
itself. Often words do or eventually will have exactly opposite or
wholly different meanings from their origins.
English has no foreign speakers. English has no single country or
nation. It has always historically been a mix-in. There is not and
never has been such a concept as a pure language.
Thus, nobody is trivializing any language. Native speakers of any
given language will often and naturally take it for granted.
Sallust, Cicero and Caesar would hardly have been shocked by Latin
pidgins and creoles. Those existed in their time. It was always an
effect of expansion of communication and interaction, all provided
primarily by advancing and spreading technology (roads, writing,
commerce) and a little dash of imperialism to boot.