[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Dynamically Creating a class method

Steve L

4/28/2006 2:08:00 AM

Hello,

I have a class called Preferance that will contain method to retreive
and set various user preferances.

If my app has 2 user defined preferances then my class might look like
the following

class Preference
def self.pref1
end
def self.pref2
end
def self.pref1=
end
def self.pref2=
end
end

Inside of each of these class methods is some sql code to retieve/set values
in my database.

The code for pref1 and pref2 is extremely similiar.

I would like to automatically generate these methods. (Because I have 50
different user preferances.)

My imaginary code would something like this

class Preference
@list_of_preference = ['max','min','timeout']
list_of_preferance.each | method_name | do
define_class_method method_name
end
end

If the above code actually worked as I imagine it, I would be able to do the
following

Preference.max =10
Preference.min = 5
Preference.timeout = 13

Also, when I needed to add a another 10 user preferances, I would simply add
some values to the @list_of_preference array.

Is this possible?

Janak


5 Answers

Trans

4/28/2006 3:13:00 AM

0

You need to get up into the "singleton" class (aka eigenclass) of the
class. The trick to that is this little esoteric gem:

(class << self; self; end)

Like this:

class Preference
@list_of_preference = ['max','min','timeout']
list_of_preferance.each | method_name | do
(class << self; self; end).class_eval {
define_method method_name
}
end
end

T.

Steve L

4/28/2006 5:52:00 AM

0

Thank you! This is very helpful and I got this code to work but I got stuck
on parameters.

How would you change this code if every generated method accepted a
parameter called new_value?

i.e the generated code would something like this

def self.pref1(new_value)
end
def self.pref2(new_value)
end
def self.pref1=(new_value)
end
def self.pref2=(new_value)
end
end

Janak

"Trans" <transfire@gmail.com> wrote in message
news:1146193983.851226.42720@i40g2000cwc.googlegroups.com...
> You need to get up into the "singleton" class (aka eigenclass) of the
> class. The trick to that is this little esoteric gem:
>
> (class << self; self; end)
>
> Like this:
>
> class Preference
> @list_of_preference = ['max','min','timeout']
> list_of_preferance.each | method_name | do
> (class << self; self; end).class_eval {
> define_method method_name
> }
> end
> end
>
> T.
>


jbonnar@gmail.com

4/28/2006 8:16:00 AM

0

define_method takes a symbol and a block. The block is bound to the
object as a method, so any arguments passed to the block can be passed
in from the newly defined method.

class Preference
@list_of_preferences = ['max', 'min', 'timeout']
@list_of_preferences.each do |name|
(class << self; self; end).class_eval do
define_method(name) { instance_variable_get(:"@#{name}") rescue
nil }
define_method(:"#{name}=") do |value|
instance_variable_set(:"@#{name}", value)
end
end
end
end

That is essentially defining an attr_accessor for each preference on
the metaclass.

class Preference
@list_of_preferences = ['max', 'min', 'timeout']
@list_of_preferences.each do |name|
(class << self; self; end).class_eval do
attr_accessor name.to_sym
end
end
end

-Justin

dsharavi

12/30/2009 7:16:00 PM

0

On Dec 29, 6:21 pm, RabbiJoekerr <jokersotherm...@yahoo.com> wrote:
> On Dec 27, 6:12 pm, xpene...@gmail.com wrote:
>
> > On Sat, 26 Dec 2009 01:12:55 -0800 (PST), docremington
>
> > <docreming...@safe-mail.net> wrote:
>
> > >tdny wrote:
> > >>http://www.ynetnews.com/articles/0,7340,L-38233...
> > >> Carter apologizes for 'stigmatizing Israel'
>
> > >Did they check him for swine flu? Fever, incoherence?
>
> > Naw. Carter's grandson is running for the State Senate in Georgia so
> > Jimmy  thinks he fake apology might snag Jewish support.

Then again, the fact that Dubai is running out of money may have
something to do with it, too.

>OTOH Jews
> > were crazy enough to vote for Obama so who knows what they'll do.
>
> There's not that many Jews in Georgia- so that's not likely.

Ahem.

Growing Number of Jews Have Georgia on Their Mind
By Anthony Weiss
March 21, 2008.

In 1999, Atlanta’s Marcus Jewish Community Center left its old
building downtown and built a lavish expansion on its 50-acre campus
in the suburbs, equipped with indoor and outdoor pools, a professional
theater, a summer camp and horse stables. Just a few years later, the
new campus is packed every night of the week, and a second suburban
campus is bustling, too. But executive director Michael Wise now says
he wishes he had the downtown building back, and maybe a new campus,
as well.

“If money were not an issue — and money is an issue — we would have
locations in four places,” Wise said.

Wise and other Jewish leaders are scrambling to cope with the
population explosion in this sprawling southern boomtown. With an
estimated 4,300 new Jews a year moving into the city, Atlanta has
become one of the fastest-growing Jewish cities in the country. That
growth is simultaneously infusing Jewish life in Atlanta with new
energy and putting a strain on the city’s institutions, as synagogues
and organizations race to accommodate the newcomers and keep them from
slipping through the cracks.

A century ago, Atlanta was a relative Jewish backwater, with a
population of about 2,000 Jews. For much of its history, Atlanta was
an uneasy place to be a Jew, as epitomized by the infamous trial and
lynching of Leo Frank in 1915 and the 1958 bombing of Atlanta’s main
Reform congregation, the Hebrew Benevolent Congregation (commonly
known as “The Temple”), in apparent retaliation for its rabbi’s
support for the civil rights movement. Jews from the rest of the
country largely kept their distance.

Even by 1976, the Jewish population was still only 21,000. But as
Atlanta has subsequently expanded in the decades since then, Jews have
poured into town with ever-increasing speed, and the population has
grown and sprawled out. According to a 2006 study conducted by Jacob
Ukeles and Ron Miller for the Jewish Federation of Greater Atlanta,
the Jewish population was 119,800 in 2006 — an increase of 56% over 10
years — and by all accounts, new residents are still coming. Atlanta
is now the 11th largest Jewish community in the United States.

Atlanta as a whole is growing faster than any metropolitan area in the
country, according to the U.S. Census Bureau. It’s fueled by an
abundance of well-paying jobs, a low cost of living and warm weather.
The growth of the Jewish population has mirrored that of Atlanta at
large, as Jews have flooded in from around the country, changing the
face of the community. Only 19% of Jewish Atlantans were born in
Georgia, compared with 30% who were born just in the New York-New
Jersey area; this means that Atlanta Jewry is losing its Southern
accent.

That growth has spawned a building boom — more than $100 million was
raised for new synagogue and community buildings over the past 10
years, according to the Jewish Federation of Greater Atlanta — as
Jewish institutions race to keep up with the growth. Where longtime
residents remember a city with five synagogues, Atlanta now has 35
congregations and six outposts of the Chabad-Lubavitch ultra-Orthodox
sect, which is famous for reaching out to unaffiliated Jews. A Jewish
high school has just opened a new building and expects to expand
within a few years.

The boom has been funded by Atlanta’s own crop of homegrown
philanthropists, which includes Home Depot founders Bernie Marcus and
Arthur Blank (now most famous as the owner of the Atlanta Falcons).
Perhaps appropriately, Blank grew up in Queens, Marcus in Newark, N.J.

Meanwhile, those unable to keep up with the building boom have found
ways to make do. Smaller synagogues that are too new or too small to
afford their own building have turned for space to one of Atlanta’s
most abundant resources: churches.

Rabbi Analia Bortz of the Conservative Congregation Or Hadash, which
for some time met at a church, recalls that her daughter complained to
her after visiting another congregation: “Mommy, it’s not fair — their
church is prettier than our church.”

Or Hadash has since outgrown its church and is now renting space in
the new, austere concrete buildings of a Jewish high school, the Weber
School. It’s an interim arrangement: In a few years, the school and
the synagogue expect to outgrow each other, and Or Hadash will likely
move yet again.

But Atlanta is also famous for its sprawl and its traffic, and as the
new Jewish population has fanned out across the city, it has become
harder to keep the community connected.

“Until you’re on the ground, it’s difficult to comprehend the scope of
the city,” said Wise, who arrived last year from Akron, Ohio.

Unlike many older cities, such as Baltimore and Cleveland, Atlanta has
no “Jewish” part of town. Jews have spread out to the north, the east
and the west of the city, and now, younger Jews are moving back into
the downtown area, too. That has made it harder for traditional Jewish
organizations to reach their target audiences. Instead, the Jewish
agencies have opened up satellite branches in the outer reaches of the
suburbs, sometimes sharing space with synagogues. Wise is now looking
to rent new space in the city, and in the far-flung northern suburbs,
to expand the JCC’s early childhood programs.

Yet, Atlanta’s largest Jewish community — the unaffiliated — dwarfs
all the building and expansion. Of Atlanta’s Jews, only 42% are
members of a synagogue or Jewish organization, one of the lowest rates
in the country (though well above the national low of 21%, in Las
Vegas), which means that some 70,000 Jews have no official connection
to the Jewish communities.

This is the flip side of Atlanta’s growth. Atlanta’s Jews are
generally transient and young, and both of these groups are generally
less likely to affiliate. Many young, professional Jews have moved
toward the central part of Atlanta, and Jewish groups have scrambled
to find ways to connect to them, often in less conventional venues.
The JCC now sponsors events in trendy bars and restaurants. Some young
Jews have taken to organizing themselves. A group of 20- and 30-
somethings has organized a monthly prayer group that meets in an
athletic clubhouse, and for those just looking to have a drink, there
is a social group that meets once a month.

With growth in Atlanta projected to continue unabated, Jewish leaders
are still grappling with the challenges they face, great and small.

“There is so much vibrancy, there are so many things happening — how
to coordinate it?” federation president Marty Kogon said. “The needs
facing the community are dramatic in a variety of fashions.”

Such as?

“Still don’t have good bagels.”
http://www.forward.com/artic...

Deborah

RabbiJoekerr

1/3/2010 2:50:00 AM

0

On Dec 30 2009, 2:15 pm, dsharavi <dshara...@hotmail.com> wrote:
> On Dec 29, 6:21 pm, RabbiJoekerr <jokersotherm...@yahoo.com> wrote:
>
> > On Dec 27, 6:12 pm, xpene...@gmail.com wrote:
>
> > > On Sat, 26 Dec 2009 01:12:55 -0800 (PST), docremington
>
> > > <docreming...@safe-mail.net> wrote:
>
> > > >tdny wrote:
> > > >>http://www.ynetnews.com/articles/0,7340,L-38233...
> > > >> Carter apologizes for 'stigmatizing Israel'
>
> > > >Did they check him for swine flu? Fever, incoherence?
>
> > > Naw. Carter's grandson is running for the State Senate in Georgia so
> > > Jimmy  thinks he fake apology might snag Jewish support.
>
> Then again, the fact that Dubai is running out of money may have
> something to do with it, too.
>
> >OTOH Jews
> > > were crazy enough to vote for Obama so who knows what they'll do.
>
> > There's not that many Jews in Georgia- so that's not likely.
>
> Ahem.
>
> Growing Number of Jews Have Georgia on Their Mind
> By Anthony Weiss
> March 21, 2008.
>
> In 1999, Atlanta’s Marcus Jewish Community Center left its old
> building downtown and built a lavish expansion on its 50-acre campus
> in the suburbs, equipped with indoor and outdoor pools, a professional
> theater, a summer camp and horse stables. Just a few years later, the
> new campus is packed every night of the week, and a second suburban
> campus is bustling, too. But executive director Michael Wise now says
> he wishes he had the downtown building back, and maybe a new campus,
> as well.
>
> “If money were not an issue — and money is an issue — we would have
> locations in four places,” Wise said.
>
> Wise and other Jewish leaders are scrambling to cope with the
> population explosion in this sprawling southern boomtown. With an
> estimated 4,300 new Jews a year moving into the city, Atlanta has
> become one of the fastest-growing Jewish cities in the country. That
> growth is simultaneously infusing Jewish life in Atlanta with new
> energy and putting a strain on the city’s institutions, as synagogues
> and organizations race to accommodate the newcomers and keep them from
> slipping through the cracks.
>
> A century ago, Atlanta was a relative Jewish backwater, with a
> population of about 2,000 Jews. For much of its history, Atlanta was
> an uneasy place to be a Jew, as epitomized by the infamous trial and
> lynching of Leo Frank in 1915 and the 1958 bombing of Atlanta’s main
> Reform congregation, the Hebrew Benevolent Congregation (commonly
> known as “The Temple”), in apparent retaliation for its rabbi’s
> support for the civil rights movement. Jews from the rest of the
> country largely kept their distance.
>
> Even by 1976, the Jewish population was still only 21,000. But as
> Atlanta has subsequently expanded in the decades since then, Jews have
> poured into town with ever-increasing speed, and the population has
> grown and sprawled out. According to a 2006 study conducted by Jacob
> Ukeles and Ron Miller for the Jewish Federation of Greater Atlanta,
> the Jewish population was 119,800 in 2006 — an increase of 56% over 10
> years — and by all accounts, new residents are still coming. Atlanta
> is now the 11th largest Jewish community in the United States.
>
> Atlanta as a whole is growing faster than any metropolitan area in the
> country, according to the U.S. Census Bureau. It’s fueled by an
> abundance of well-paying jobs, a low cost of living and warm weather.
> The growth of the Jewish population has mirrored that of Atlanta at
> large, as Jews have flooded in from around the country, changing the
> face of the community. Only 19% of Jewish Atlantans were born in
> Georgia, compared with 30% who were born just in the New York-New
> Jersey area; this means that Atlanta Jewry is losing its Southern
> accent.
>
> That growth has spawned a building boom — more than $100 million was
> raised for new synagogue and community buildings over the past 10
> years, according to the Jewish Federation of Greater Atlanta — as
> Jewish institutions race to keep up with the growth. Where longtime
> residents remember a city with five synagogues, Atlanta now has 35
> congregations and six outposts of the Chabad-Lubavitch ultra-Orthodox
> sect, which is famous for reaching out to unaffiliated Jews. A Jewish
> high school has just opened a new building and expects to expand
> within a few years.
>
> The boom has been funded by Atlanta’s own crop of homegrown
> philanthropists, which includes Home Depot founders Bernie Marcus and
> Arthur Blank (now most famous as the owner of the Atlanta Falcons).
> Perhaps appropriately, Blank grew up in Queens, Marcus in Newark, N.J.
>
> Meanwhile, those unable to keep up with the building boom have found
> ways to make do. Smaller synagogues that are too new or too small to
> afford their own building have turned for space to one of Atlanta’s
> most abundant resources: churches.
>
> Rabbi Analia Bortz of the Conservative Congregation Or Hadash, which
> for some time met at a church, recalls that her daughter complained to
> her after visiting another congregation: “Mommy, it’s not fair — their
> church is prettier than our church.”
>
> Or Hadash has since outgrown its church and is now renting space in
> the new, austere concrete buildings of a Jewish high school, the Weber
> School. It’s an interim arrangement: In a few years, the school and
> the synagogue expect to outgrow each other, and Or Hadash will likely
> move yet again.
>
> But Atlanta is also famous for its sprawl and its traffic, and as the
> new Jewish population has fanned out across the city, it has become
> harder to keep the community connected.
>
> “Until you’re on the ground, it’s difficult to comprehend the scope of
> the city,” said Wise, who arrived last year from Akron, Ohio.
>
> Unlike many older cities, such as Baltimore and Cleveland, Atlanta has
> no “Jewish” part of town. Jews have spread out to the north, the east
> and the west of the city, and now, younger Jews are moving back into
> the downtown area, too. That has made it harder for traditional Jewish
> organizations to reach their target audiences. Instead, the Jewish
> agencies have opened up satellite branches in the outer reaches of the
> suburbs, sometimes sharing space with synagogues. Wise is now looking
> to rent new space in the city, and in the far-flung northern suburbs,
> to expand the JCC’s early childhood programs.
>
> Yet, Atlanta’s largest Jewish community — the unaffiliated — dwarfs
> all the building and expansion. Of Atlanta’s Jews, only 42% are
> members of a synagogue or Jewish organization, one of the lowest rates
> in the country (though well above the national low of 21%, in Las
> Vegas), which means that some 70,000 Jews have no official connection
> to the Jewish communities.
>
> This is the flip side of Atlanta’s growth. Atlanta’s Jews are
> generally transient and young, and both of these groups are generally
> less likely to affiliate. Many young, professional Jews have moved
> toward the central part of Atlanta, and Jewish groups have scrambled
> to find ways to connect to them, often in less conventional venues.
> The JCC now sponsors events in trendy bars and restaurants. Some young
> Jews have taken to organizing themselves. A group of 20- and 30-
> somethings has organized a monthly prayer group that meets in an
> athletic clubhouse, and for those just looking to have a drink, there
> is a social group that meets once a month.
>
> With growth in Atlanta projected to continue unabated, Jewish leaders
> are still grappling with the challenges they face, great and small.
>
> “There is so much vibrancy, there are so many things happening — how
> to coordinate it?” federation president Marty Kogon said. “The needs
> facing the community are dramatic in a variety of fashions.”
>
> Such as?
>
> “Still don’t have good bagels.”http://www.forward.com/artic...
>
> Deborah

LOL well wonders never cease!