[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

best way to dynamically create new instance methods

Dan Tenenbaum

12/11/2006 8:28:00 PM

Hi, I've been sifting through module_eval, instance_eval, define_method,
and the like and I can't seem to accomplish what I am trying to do,
which is something like this:

class Foo
def initialize
list_of_method_names =
get_list_of_method_names_from_some_external_function
for method_name in list_of_method_names_do
create_method(method_name)
end
end

def create_method(method_name)
# some magic here as yet unknown
end

end

The list retrieval is in initialize() because the list is constantly
changing so I always want the most recent one when I initialize my
class.

What I want create_method() to do (simplified here) is, given the
argument "baz", add a method to the current instance of Foo that would
look like this if it were a traditional method (that is, not dynamically
created):

def baz(*args)
args[0]
end

That seems pretty simple but I have run into all sorts of problems with
various ways of doing it. Rather than recount all those ways and why
they didn't work, I instead present the minimal example of the problem I
am trying to solve and hope that someone can solve it.

Thanks in advance.
Dan

--
Posted via http://www.ruby-....

3 Answers

Paul Lutus

12/11/2006 9:21:00 PM

0

Dan Tenenbaum wrote:


> That seems pretty simple but I have run into all sorts of problems with
> various ways of doing it. Rather than recount all those ways and why
> they didn't work, I instead present the minimal example of the problem I
> am trying to solve and hope that someone can solve it.

Rather than try to sort out a way to create methods on the fly, perhaps it
would be better if you were to explain what you are trying to do ... what
problem does this approach solve?

There are any number of ways to produce results for given input data, and
creating new methods for a class at runtime is not necessarily the most
efficient way to accomplish your goal.

What I am saying is you are now struggling to solve, not the original
problem your program is meant to solve, but the problems created by your
solution.

One possible alternative is a hash with keys consisting of identifying
labels, and values consisting of lambdas that can produce various kinds of
results. The lambdas could easily stand in for your custom methods, and
this approach might end up being easier to implement.

Example:

----------------------------------------

#!/usr/bin/ruby -w

hash = {
"+" => lambda { |x,y| x + y },
"-" => lambda { |x,y| x - y },
"*" => lambda { |x,y| x * y },
"/" => lambda { |x,y| x / y },
}

puts hash["/"].call(1.0,3.0)

----------------------------------------

Output:

0.333333333333333

Depending on what you are trying to do, this might be easier to use and
might be faster as well, especially if you take your development time into
account.

--
Paul Lutus
http://www.ara...

James Gray

12/11/2006 10:15:00 PM

0

On Dec 11, 2006, at 2:28 PM, Dan Tenenbaum wrote:

> What I want create_method() to do (simplified here) is, given the
> argument "baz", add a method to the current instance of Foo that would
> look like this if it were a traditional method (that is, not
> dynamically
> created):
>
> def baz(*args)
> args[0]
> end

>> class Dynamic
>> def initialize(name)
>> create_method(name)
>> end
>> private
>> def create_method(name)
>> class << self; self end.send(:define_method, name) { |*args|
args.first }
>> end
>> end
=> nil
>> foo = Dynamic.new(:foo)=> #<Dynamic:0x71dbd4>
>> bar = Dynamic.new(:bar)
=> #<Dynamic:0x71b3fc>
>> foo.foo("Hi")
=> "Hi"
>> foo.bar("Hi")
NoMethodError: undefined method `bar' for #<Dynamic:0x71dbd4>
from (irb):33
>> bar.bar("Hi")
=> "Hi"

Hope that helps.

James Edward Gray II

RD Sandman

4/1/2013 11:48:00 PM

0

GOP_Decline_and_Fall <Dev@null.net> wrote in
news:61qhl8tqv546empfqt9a44aeben971qq6r@4ax.com:

> On Sun, 31 Mar 2013 18:31:07 -0700 (PDT), bigdog
> <jecorbett1951@yahoo.com> wrote:
>
>>On Sunday, March 31, 2013 9:10:54 PM UTC-4, GOP_Decline_and_Fall
>>wrote:
>>> On Sun, 31 Mar 2013 17:49:36 -0700 (PDT), bigdog
>>>
>>> <jecorbett1951@yahoo.com> wrote:
>>>
>>>
>>>
>>> >On Sunday, March 31, 2013 7:09:18 PM UTC-4, GOP_Decline_and_Fall
>>> >wrote:
>>>
>>> >> On Sun, 31 Mar 2013 15:28:58 -0700 (PDT), bigdog
>>>
>>> >>
>>>
>>> >> <jecorbett1951@yahoo.com> wrote:
>>>
>>> >>
>>>
>>> >> >On Sunday, March 31, 2013 5:54:19 PM UTC-4, GOP_Decline_and_Fall
>>> >> >wrote:
>>>
>>> >> >> Even if nobody gives a flying fuck about the putrid sick
>>> >> >> living
>>>
>>> >> >> conditions of factory farm animals ??? how can they eat the
>>> >> >> poisoned
>>>
>>> >> >> ???food??? that comes out of those hellholes?
>>>
>>> >>
>>>
>>> >> >They taste good.
>>>
>>> >>
>>>
>>> >> That's just the chemicals put in to mask the true taste.
>>>
>>> >
>>>
>>> >Must be damn good chemicals.
>>>
>>> >>
>>>
>>> >> Are we to take that you aren't a hunter, never buy organic
>>>
>>> >> and have never tasted real meat then?
>>>
>>> >>
>>>
>>> >You got two out of three right. I'm not a hunter. I don't buy
>>> >organic because I don't like to pay extra for bullshit. I'm quite
>>> >sure all that animal flesh I've eater during the past 60 years is
>>> >real meat. I have eaten venison that somebody else killed and
>>> >thought it was very bland tasting. I'll take a good cut of prime
>>> >rib a nice rack of baby back ribs any day.
>>>
>>> >>
>>>
>>> >>
>>>
>>> >> You should try it, it's really quite different to factory farmed
>>> >> and
>>>
>>> >> unless you are poor well worth a few extra bucks as a splurge.
>>>
>>> >>
>>>
>>> >> With all the money you spend on guns and ammo I would have
>>> >> thought you
>>>
>>> >> could afford to eat real food occasionally.
>>>
>>> >
>>>
>>> >The food I eat is quite real and tastes damn good. But then I'm not
>>> >an eco-snob.
>>>
>>>
>>>
>>> Nothing to do with snobbery, eco or other.
>>>
>>>
>>>
>>> The mushrooming epidemic of food borne disease and antibiotic
>>>
>>> saturated meat shows no respect for of any particular social status.
>>>
>>>
>>>
>>>
>>>
>>> http://www.cnn.com/2009/OPINION/10/26/opinion.jona...
>>>
>>>
>>>
>>>
>>>
>>> The same conditions that lead at least 76 million Americans to
>>> become
>>>
>>> ill from their food annually and that promote antimicrobial
>>> resistance
>>>
>>> also contribute to the risk of a pandemic.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> Similar conclusions were reached by the Council for Agricultural
>>>
>>> Science and Technology, which brought together industry experts and
>>>
>>> experts from the WHO, OIE and USDA. Their 2005 report argued that a
>>>
>>> major impact of factory farming is "the rapid selection and
>>>
>>> amplification of pathogens that arise from a virulent ancestor
>>>
>>> (frequently by subtle mutation), thus there is increasing risk for
>>>
>>> disease entrance and/or dissemination."
>>>
>>>
>>>
>>> Breeding genetically uniform and sickness-prone birds in the
>>>
>>> overcrowded, stressful, feces-infested and artificially lit
>>> conditions
>>>
>>> of factory farms promotes the growth and mutation of pathogens. The
>>>
>>> "cost of increased efficiency," the report concludes, is increased
>>>
>>> global risk for diseases. Our choice is simple: cheap chicken or our
>>>
>>> health.
>>>
>>>
>>>
>>> Today, the factory farm-pandemic link couldn't be more lucid. The
>>>
>>> primary ancestor of the recent H1N1 swine flu outbreak originated at
>>> a
>>>
>>> hog factory farm in America's most hog-factory-rich state, North
>>>
>>> Carolina, and then quickly spread throughout the Americas.
>>>
>>>
>>>
>>> It was in these factory farms that scientists saw, for the first
>>> time,
>>>
>>> viruses that combined genetic material from bird, pig and human
>>>
>>> viruses. Scientists at Columbia and Princeton Universities have
>>>
>>> actually been able to trace six of the eight genetic segments of the
>>>
>>> most feared virus in the world directly to U.S. factory farms.
>>>
>>>
>>>
>>> Perhaps in the back of our minds we already understand, without all
>>>
>>> the science, that something terribly wrong is happening. We know
>>> that
>>>
>>> it cannot possibly be healthy to raise such grotesque animals in
>>> such
>>>
>>> grossly unnatural conditions. We know that if someone offers to show
>>>
>>> us a film on how our meat is produced, it will be a horror film.
>>>
>>>
>>>
>>> We perhaps know more than we care to admit, keeping it down in the
>>>
>>> dark places of our memory -- disavowed. When we eat factory-farmed
>>>
>>> meat, we live on tortured flesh. Increasingly, those sick animals
>>> are
>>>
>>> making us sick.
>>
>>OK, you're not an eco-snob. You're a germaphobe.
>
> I just like real food occasionally.
>
> Don't knock it if you haven't tried it.
>

I have, but, like Bigdog, I will eat whatever I like. I am an omnivore.
Let's eat!!!

--
Sleep well, tonight.....

RD (The Sandman

If you take out the murders, Washington has
a very low crime rate!