[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Dynamic methods

-j b-

8/24/2007 1:47:00 AM

I have seen examples of catching unkown methods called on a class with
method_missing.
For example:

class Example
def initialize(...)
#...
end

def method_missing(item)
call = item.id2name
process(call)
end
end

But I'm not sure if that is an 'acceptable' way to do things. Also,
suppose that I wanted to add a method dynamically that takes arguments,
is this even possible?
My general thought is allowing functionality to be added to a program
dynamically based on a set of rules in a config file.
Any suggestions are welcome.
Thank you.
--
Posted via http://www.ruby-....

5 Answers

Trans

8/24/2007 4:29:00 AM

0



On Aug 23, 6:46 pm, -j B- <jrb...@drexel.edu> wrote:
> I have seen examples of catching unkown methods called on a class with
> method_missing.
> For example:
>
> class Example
> def initialize(...)
> #...
> end
>
> def method_missing(item)
> call = item.id2name
> process(call)
> end

def method_missing(item, *args, &blk)
process(item, *args, &blk) # item is already a symbol
end

> end
>
> But I'm not sure if that is an 'acceptable' way to do things. Also,
> suppose that I wanted to add a method dynamically that takes arguments,
> is this even possible?
> My general thought is allowing functionality to be added to a program
> dynamically based on a set of rules in a config file.
> Any suggestions are welcome.
> Thank you.

Also, keep in mind Ruby classes are open. You can add real methods to
them on the fly.

class Example
end

foo = "bar"

Example.class_eval %{
def #{foo}
"#{foo}"
end
}

Example.new.foo #=> "bar"

T.


Marcin Mielzynski

8/24/2007 10:54:00 AM

0

Trans wrote:

> Also, keep in mind Ruby classes are open. You can add real methods to
> them on the fly.
>
> class Example
> end
>
> foo = "bar"
>
> Example.class_eval %{
> def #{foo}
> "#{foo}"
> end
> }
>

It's better not to use string evals:

Example.class_eval do
define_method foo do
"#{foo}"
end
end

lopex

Robert Dober

8/24/2007 11:37:00 AM

0

On 8/24/07, Robert Dober <robert.dober@gmail.com> wrote:
>
> class A
> class << self
> alias_method :old_new, :new
> def new *args, &block
> o = old_new( *args, &block )
> o.instance_eval do
> def ara; value end
> end
> o
> end
> end
> end ## this does the trick, simple concise, trivial :( well we wish it were.
> ## and there are issues with the alias it is a potential redefeinition disaster
the above really su..., sorry

class A
alias_method :old_init, :initialize
def initialize *args, &blk
old_init( *args, &blk )
instance_eval do
def ara; value end
end
end
end
# the issues remain though, we could apply the method redefintion
hammer Pit and Ara came up with and just redefine initialize like
that, but that would make it about 100 lines I guess :(

Robert
--
I'm an atheist and that's it. I believe there's nothing we can know
except that we should be kind to each other and do what we can for
other people.
-- Katharine Hepburn

last_permutation

2/17/2011 8:06:00 PM

0

On Feb 17, 3:01 pm, dsharavi <dshara...@hotmail.com> wrote:
> On Feb 17, 11:32 am, P-Dub <pwolf...@hotmail.com> wrote:
>
>
>
> > On 2/17/2011 2:10 PM, John Manning wrote:
>
> > > In dramatic turnaround, US to censure Israel in Security Council
>
> > > -- WASHINGTON – In a dramatic departure from longstanding policy,
> > > the United States intends to support a United Nations Security
> > > Council resolutions censuring Israel for building settlements
> > > in Palestinian territory.
>
> > > The Obama administration told Arab governments Tuesday it will back
> > > a draft resolution saying the Security Council "does not accept
> > > the legitimacy of continued Israeli settlement activity," according
> > > to Foreign Policy magazine.
>
> > > The language, which was confirmed to Foreign Policy by two
> > > Security Council diplomats, calls the Israeli settlements "a
> > > serious obstacle to the peace process."
>
> > > It says the Security Council "condemns all forms of violence,
> > > including rocket fire from Gaza, and stresses the need for calm
> > > and security for both peoples."
>
> > > The Obama administration has used similar language to criticize
> > > Israel on settlements, but has not supported any such UN
> > > resolution condemning Israel. Doing so would constitute a
> > > significant US policy shift towards its ally.
>
> > > But the US also intends to veto a stronger non-binding resolution
> > > by the 15-member body denouncing the Israeli settlements as "illegal."
>
> > > The new language was part of a compromise put forth by Susan Rice, the
> > > US Ambassador to the UN.
>
> > > The earlier resolution was offered by the Palestinian Authority
> > > and garnered nearly 120 co-sponsors, all Arab and non-aligned
> > > states. UN diplomats told Reuters it would probably be approved by
> > > all Security Council members other than the US.
>
> > > It says that "Israeli settlements established in the
> > > Palestinian Territory occupied since 1967, including East
> > > Jerusalem, are illegal and constitute a major obstacle to
> > > the achievement of a just, lasting and comprehensive peace."
>
> > > The US has unilaterally vetoed or declined to support dozens of
> > > UN Security Council resolutions condemning Israel during the
> > > last several decades. For the US to support even the less harsh language
> > > is a remarkable development.
>
> > > Palestinian leaders reportedly rejected the US offer on the
> > > new resolution, seeking instead to push for a vote Friday on
> > > the original language designating the Israel settlements "illegal."
> > > It raised the prospect that the Obama administration would cast its
> > > first Security Council veto.
>
> > >http://www.rawstory.com/rs/2011/02/17/in-dramatic-turnaroun.......
>
> > I think we need to censure the United States of America. There are
> > apparently illegally established 'settlements' on Indian land that was
> > 'usurped' in a massive 'land-grab' by settlers a long time ago.
>
> > Perhaps all the people living on these American 'settlements' need to
> > leave their homes as soon as possible, and give them over to the Indians.
>
> > P-Dub: Unless the 'Palestinians' are actually willing to NEGOTIATE
> > (e.g., this includes COMPROMISE), then Israel should not give them one
> > inch of this so-called 'Palestinian' land.
>
> What's the fuss over this so-called "censure"

As one from your own tribe wrote, Times They are a Changing.

dsharavi

2/17/2011 8:28:00 PM

0

On Feb 17, 11:57 am, Just Wondering <fmh...@comcast.net> wrote:
> On 2/17/2011 12:32 PM, P-Dub wrote:
>
> > On 2/17/2011 2:10 PM, John Manning wrote:
>
> >> In dramatic turnaround, US to censure Israel in Security Council
>
> >> -- WASHINGTON – In a dramatic departure from longstanding policy,
> >> the United States intends to support a United Nations Security
> >> Council resolutions censuring Israel for building settlements
> >> in Palestinian territory.
>
> >> The Obama administration told Arab governments Tuesday it will back
> >> a draft resolution saying the Security Council "does not accept
> >> the legitimacy of continued Israeli settlement activity," according
> >> to Foreign Policy magazine.
>
> >> The language, which was confirmed to Foreign Policy by two
> >> Security Council diplomats, calls the Israeli settlements "a
> >> serious obstacle to the peace process."
>
> >> It says the Security Council "condemns all forms of violence,
> >> including rocket fire from Gaza, and stresses the need for calm
> >> and security for both peoples."
>
> >> The Obama administration has used similar language to criticize
> >> Israel on settlements, but has not supported any such UN
> >> resolution condemning Israel. Doing so would constitute a
> >> significant US policy shift towards its ally.
>
> >> But the US also intends to veto a stronger non-binding resolution
> >> by the 15-member body denouncing the Israeli settlements as "illegal."
>
> >> The new language was part of a compromise put forth by Susan Rice, the
> >> US Ambassador to the UN.
>
> >> The earlier resolution was offered by the Palestinian Authority
> >> and garnered nearly 120 co-sponsors, all Arab and non-aligned
> >> states. UN diplomats told Reuters it would probably be approved by
> >> all Security Council members other than the US.
>
> >> It says that "Israeli settlements established in the
> >> Palestinian Territory occupied since 1967, including East
> >> Jerusalem, are illegal and constitute a major obstacle to
> >> the achievement of a just, lasting and comprehensive peace."
>
> >> The US has unilaterally vetoed or declined to support dozens of
> >> UN Security Council resolutions condemning Israel during the
> >> last several decades. For the US to support even the less harsh language
> >> is a remarkable development.
>
> >> Palestinian leaders reportedly rejected the US offer on the
> >> new resolution, seeking instead to push for a vote Friday on
> >> the original language designating the Israel settlements "illegal."
> >> It raised the prospect that the Obama administration would cast its
> >> first Security Council veto.
>
> >>http://www.rawstory.com/rs/2011/02/17/in-dramatic-turnaroun......
>
> > I think we need to censure the United States of America. There are
> > apparently illegally established 'settlements' on Indian land that was
> > 'usurped' in a massive 'land-grab' by settlers a long time ago.
>
> > Perhaps all the people living on these American 'settlements' need to
> > leave their homes as soon as possible, and give them over to the Indians.
>
> > P-Dub: Unless the 'Palestinians' are actually willing to NEGOTIATE
> > (e.g., this includes COMPROMISE), then Israel should not give them one
> > inch of this so-called 'Palestinian' land.
>
> And while they're at it, censure Norway, Denmark, and Sweden for the
> Viking occupation of Normandy.  Then censure France for the Norman
> occupation of Britain.  Then censure France and Britain for the British
> occupation of North America.  Then censure Spain for its occupation of
> Mexican territory, Portugal for its occupation of Brazil, China for its
> occupation of Tibet.  Hell's Bells, there isn't a country in the world
> who isn't at least partially occupied by the descendants of invaders
> from other areas.

One need not go so far. Let the UN censure such bastions of human
rights as Cuba, Iran, Syria, Saudi Arabia, Pakistan, and so on and so
forth.

Oh wait, the UN is going to go along with those guys against the only
democracy in the Middle East? the only county which has a free press?
the only Middle East country which doesn't persecute/judicially murder
gay men? And we're supporting this?

I'd check the source of this alleged "censure", or "rebuke", whatever
it is.

> Israel would have never occupied those occupied territories, except for
> neighboring countries who instituted war against Israel in an attempt to
> wipe it off the face of the earth.  But while the U.N. is at it, how
> about censuring England for partitioning Israel off as a separate nation
> in the first place?  Oh, wait, the U.N. already approved the partition.
> So maybe the U.N. ought to be censuring itself.
>
Better yet: the US ought to be censuring the UN in the only way the UN
understands: make them pay rent.

> If the Arab world was actually sympathetic to the Arabian population in
> occupied territories, Syria and Jordan would welcome them into their
> borders, maybe even partition a chunk of land and call it Palestine for
> it.  Instead, they use those people as a political football.  

Political football? In part, of course, but Jordan wasn't using Arab
Palestinians as political footballs when it massacred over 20,000 of
them in less than a week. Neither was Kuwait using them as political
footballs when it expelled almost half a million of them (and detained
and tortured some 6,000 of them) for their support of Saddam. The
reasons those respective governments gave for the mass
slaughter/"ethnic cleasning"?

Jordan: "They [Arafat and Arab Palestinians] were saying, in effect,
that we did not exist. We could not allow that to happen."

Kuwait: "If people pose a security threat, as a sovereign country we
have the right to exclude anyone we don't want."

Were either censured in the UN for these actions? Does the pope wear a
hat? Does a cockhorse have a horsecock? Does a frog have a watertight
butt? Does a bear shi'ite in the woods?

>Whatever
> you may think of Israel's occupation of the West Bank and Golan Heights,
> the position Israel's Arab Nations is despicable.

They believe devoutly that wrongs, injustices, and abuses are not
wrongs, injustices, and abuses whenever committed by decent men like
themselves. And I use "men" deliberately.

"The injury we do, and the one we suffer, are not weighed in the same
scales" Aesop

Deborah