[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Can I make a new function like attr_accessor? (solved

Jack, Paul

12/15/2004 10:28:00 PM

> BTW, if you're just coming from Ruby to Java, I wrote an
> article on my blog that you might find really useful:
> http://fhwang.net/bl...

Thanks. If nothing else, I've changed to underscores for
my methods...

I've got it all working nicely now. I used Tom's sample
code as a starting point. Thanks, Tom!

The Observable pattern that itsme213 recommended doesn't
seem to fit my needs since I need multiple listeners per
emitter but maybe that's just Java think...

Anyway, here's the code I ended up using, in case anyone
else wants to do this sort of thing. (I tried defining
attr_emitter as "def self.attr_emitter" in the Emitter
module, but that didn't work. It seems like you have
to define these types of things in the Module class?)

Thanks everyone for your help!

-Paul




class Module

def attr_emitter(*attrs)
attrs.each do |attr|
class_eval %{

def #{attr}
@#{attr}
end

def #{attr}=(x)
old = @#{attr}
@#{attr} = x
emit(:#{attr}, old)
end
}
end
end

end


module Emitter

def add_listener(listener)
raise "Invalid listener" unless listener.respond_to? :property_changed
@emitter_listeners = [] unless @emitter_listeners
@emitter_listeners.push(listener)
self
end

def remove_listener(listener)
return self unless @emitter_listeners
@emitter_listeners.delete(listener)
self
end

def emit(sym, oldValue)
return self unless @emitter_listeners
@emitter_listeners.each { |x| x.property_changed(self, sym, oldValue) }
self
end

end




6 Answers

James Gray

12/15/2004 10:33:00 PM

0

On Dec 15, 2004, at 4:28 PM, Jack, Paul wrote:

> The Observable pattern that itsme213 recommended doesn't
> seem to fit my needs since I need multiple listeners per
> emitter but maybe that's just Java think...

Observable does support multiple listeners, just FYI.

James Edward Gray II



Joel VanderWerf

12/15/2004 10:44:00 PM

0

James Edward Gray II wrote:
> On Dec 15, 2004, at 4:28 PM, Jack, Paul wrote:
>
>> The Observable pattern that itsme213 recommended doesn't
>> seem to fit my needs since I need multiple listeners per
>> emitter but maybe that's just Java think...
>
>
> Observable does support multiple listeners, just FYI.
>
> James Edward Gray II
>

If he's talking about standard ruby Observable, then yes it does support
multiple listeners.

If he's talking about my inaptly named Observable module (shoulda been
ObservableAttrs) then the answer is more subtle.

You can have multiple listeners, as long as the listeners are distinct
object, rather than multiple listener clauses for one object. In fact,
multiple listener clauses for an object are perfectly fine too, as long
as they are listening for distinct patterns. The following example has
two listeners, each with two "when" clauses:

require 'observable'

class Speaker
extend Observable
observable :foo
def run
self.foo = "1"
self.foo = "2"
self.foo = "3"
end
end

class Listener
def initialize(speaker)
speaker.when_foo /2/ do |v|
puts "saw a 2 in #{v.inspect}"
end
speaker.when_foo /\d/ do |v|
puts "saw a digit in #{v.inspect}"
end
# This would override the first when_foo clause
#speaker.when_foo /2/ do |v|
# puts "overridden"
#end
end
end

spk = Speaker.new
lst = [Listener.new(spk), Listener.new(spk)]

spk.run

OUTPUT

saw a digit in "1"
saw a digit in "1"
saw a 2 in "2"
saw a digit in "2"
saw a digit in "2"
saw a 2 in "2"
saw a digit in "3"
saw a digit in "3"


Ashikaga

5/7/2007 3:51:00 PM

0

On 144khz Dalboz Dragon shrieked:
> erimess earned a wedgie by saying:
>
>>Been there, done that. Yeah, I worked for a guy once who thought
>>everyone was a robot and basically said they were lazy if they didn't
>>want to work overtime after having worked 10 12-hour days in a row.
>>Not that *he* ever worked that many hours. (Though he did actually
>>work, unlike my most recent boss. Funny how I say that when I have
>>two jobs, but for some reason never think of my bosses as my bosses.
>>Which might say something about them, or maybe cause I always think of
>>my "last job" as being the last actual accounting job I ever had.) I
>>would not blame you at all for wanting to get out of that situation.
>>BTW, I've found that "holding that power" doesn't always mean a whole
>>lot. Even if somewhere inside someone knows you're in that kind of
>>position, consciously they don't necessarily know it or deal with it,
>>and it can end up meaning nothing. They will notice more if you leave
>>and you can have a sort of post-satisfaction. :-)
>
> Oh, it would be. I'm the only one in the entire company who knows how
> to perform what has become a vital service. And everyone has too much
> work right. Letting anyone go is going to make the office collapse
> under its own weight. At least I could watch from a distance.
>
> The job used to be much better, and I felt about my boss the way you
> feel about your bosses. She didn't really feel like a boss, and was
> actually a close friend. I saw her through rough personal patch for
> her when she was originally getting together with her wife (yes, I
> said that right, trust me). After company was "merged," she has pretty
> much checked out, and the cult of personality is gone. People are
> dropping left and right, and they haven't been hiring anyone. So it
> just gets progressively worse.

The boss who doesn't feel like a boss is the best boss. It's what we
called "natural leader", but the book doesn't like to tell people what to
think, so they put it to compare with "inherited leader" the one who gets
their power with job title.

It seems your boss probably was coping with changes too. If the management
changed, she might not know her boss as well as before, so it's safer for
her to deal it "professionally" so it may be a reason why she became more
formal, and that can alienate some workers.

Though I hate to relate back to my work experience, but I think it's easier
to assess a situation by scenario. One of the ways my ex-job's boss to
ruin the company culture was to replace all those buddy-buddy sales coaches
with policy-following people. And that's who she was. Why would we want
another person of that persona? It is a diverse work place, and we need
all kind of management styles to keep everyone happy. And if one follows
the textbook, it's managers' job to "break the rule" but not following them
mindlessly. The reality check..., people fear losing their jobs, so they'd
do stuff not for the best interest of the company. I've mentioned to her
that a company needs different kinds of sales to cater different customers
(and that's what Gap was aiming for, if she has ever read our employee
coaching package), as a way to suggest that should be how management is
too, but I guess my messages are often too polite and cryptic for her. She
continues to fire people who are not "professional looking" which pissed me
off. Do we really want to cater only one type of customers? It's really
amazing one person can single-handedly changed so much company culture....

<snip>
>>That's a lot to do with the school. Not in Ohio so I don't know, but
>>there might even be state rules about it. It will also have a huge
>>affect on financial aide if you try to get it. Well, it will
>>definitely be a problem with like a Pell Grant or something like that.
>>State grants will depend on the state. Scholarships are an entirely
>>different matter, or anything coming directly from the school. But
>>it's something you need to think about.
>
> It's something I've been keeping in mind, which is why I'm going to
> start with a nice cheap community college to explore my options and
> get the prereqs out of the way. I'll have to figure out the rest when
> I fully decide on a course.

It's nice to explore.

>>> As for what I want to pursue, I'm not sure, and I'm thinking
>>>of taking a few basic courses in different subjects to figure it out.
>>
>>Not that I don't think you should try some different courses, but
>>taking a course doesn't have much to do with whether you want to
>>actually do that thing in real life. An unfortunate reality is that
>>you never really know until you get out there and do it. You're a
>>case in point -- obviously your first degree has not gotten you any
>>satisfaction. (I don't recall what your first degree is in.)
>
> Bachelor of Arts in Cinema and Television Arts, emphasis on Multimedia
> and Animation. Unfortunatly, I was in the first class to graduate from
> that emphasis, so we were the experimental class. I didn't have the
> necessary fully developed skills or a workable portfolio, and found it
> tough to get a job without having contacts or internship experience.
> However, I've been working out in the real world for several years no,
> so I know the difference between academia and real life.

Me in the same bin. Did not take any finance-related internship so I ended
up working in other areas that I never intended to be in (sales), just to
make a living, but totally hated the job and learned what's real life and
what's academic (actually I already had some taste of it while in school
project, so I pretty much knew what NOT to do when I was at the job I kept
complaining about).

<snip>
>>> I also don't know if anything from my old coursework
>>>would transfer or if I would still need to take my general education
>>>classes over again. I'll probably be starting at the local community
>>>college (which I live right next to; convenient) to save some money in
>>>the beginning.
>>
>>That again is entirely an issue based on the school itself. I would
>>*hope* that at least your GEs would transfer, unless you go to a
>>school with some seriously bizarre requirements. Probably some of the
>>other stuff would transfer, and if nothing else, anything should
>>transfer as electives, but that's just space fillers. They'll be a
>>lot concerned about residency. Which is why you might want to be
>>careful about doing the community college bit. Between transferring
>>credits and doing classes at a community college, you might find
>>yourself not having enough required courses left over to fulfill
>>residency. So I would at least check that.
>
> I've gotten some answers from a college counselor, but without knowing
> the specifics about what I want to do, she could only advise me so
> far. The only thing she could give me specifics on was the prereqs for
> med school since those are pretty standard.

Her advice is the same if I were in her role. The final decision rests
upon you.

Before I forget, keep in mind not all community college credits are
tranferable. I believe there is a 90 quarter units cap for lower division
and almost entirely of the upper division courses offered at a two-year
college will not be acredited, but for self-improvement only. It's quite
annoying but rather crucial information.

--
Ashikaga - a28 5/7/2007 8:02:41 AM

someone

5/8/2007 8:01:00 AM

0

On Mon, 07 May 2007 15:51:07 GMT, Ashikaga <citizenashi@yahoo.com>
earned a wedgie by saying:

>Before I forget, keep in mind not all community college credits are
>tranferable. I believe there is a 90 quarter units cap for lower division
>and almost entirely of the upper division courses offered at a two-year
>college will not be acredited, but for self-improvement only. It's quite
>annoying but rather crucial information.

I'll look into that, but it might be different for grad school, since
ultimately once I decide on a course of action, my community college
time will be focused on completing prereqs for whatever graduate
program I decide on.
--
Dalboz Dragon -=(UDIC)=-
Dispenser of the Holy *SMACK!* (Not to be confused with *SLAP*)
--------------
d+++ e+ N+ T+ Om+ U1!2!47'S'9!K!L u- uC+ uF- uG++ uLB+ uA+
nC++ nH nP+ nI++ nPT nS+ nT o oA+++ y+++ a29
--------------
We are all but figments of Chuck Norris' imagination.

Ashikaga

5/9/2007 7:27:00 AM

0

On 21khz Dalboz Dragon shrieked:
> Ashikaga earned a wedgie by saying:
>
>>Before I forget, keep in mind not all community college credits are
>>tranferable. I believe there is a 90 quarter units cap for lower division
>>and almost entirely of the upper division courses offered at a two-year
>>college will not be acredited, but for self-improvement only. It's quite
>>annoying but rather crucial information.
>
> I'll look into that, but it might be different for grad school, since
> ultimately once I decide on a course of action, my community college
> time will be focused on completing prereqs for whatever graduate
> program I decide on.

And good luck with it. The bottom line is, extra skills wouldn't hurt.
Meet new and more people will certainly be fresh air.

--
Ashikaga - a29 5/9/2007 12:24:18 AM

erimess

5/12/2007 10:26:00 PM

0

On Tue, 08 May 2007 08:00:32 GMT, someone@somewhere.com (Dalboz
Dragon) wrote:

>On Mon, 07 May 2007 15:51:07 GMT, Ashikaga <citizenashi@yahoo.com>
>earned a wedgie by saying:
>
>>Before I forget, keep in mind not all community college credits are
>>tranferable. I believe there is a 90 quarter units cap for lower division
>>and almost entirely of the upper division courses offered at a two-year
>>college will not be acredited, but for self-improvement only. It's quite
>>annoying but rather crucial information.
>
>I'll look into that, but it might be different for grad school, since
>ultimately once I decide on a course of action, my community college
>time will be focused on completing prereqs for whatever graduate
>program I decide on.

Just as a quickie note, some of the students I've tutored privately
are actually in lower-level classes (sometimes at the community
college) to get a pre-req for something in grad school. In fact, I
see that a lot. So obviously this *can* be done and not with any
issue really.

--

Erimess Dragon
-==(UDIC)==-

d++e+NT++Om UK!1!2!3!A!L!
U+uCuFuG+++uLB+uA+ nC+nH+nP+nS++nT-xa5

Never compare yourself to the best others can do,
but rather to the best you can do.