[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

How to create dinamicly method

Damjan Rems

3/15/2007 1:58:00 PM

What I am trying to accomplist is:

class MyClass
def addVar(var, val)
instance_variable_set("@#{var}", val)
define_method("#{var}") { || instance_variable_get("#{var}") }
end
end

c = MyClass.new
c.addVar('aa',10) # here pops the error
c.aa # should return 10

But I get this error:
NoMethodError: undefined method `define_method' for #<MyClass:0x2d01cc0
@aa=10>
from (irb):4:in `addVar'
from (irb):8

I know this should work because it's used all over rails.

Advice me please

TheR

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

6 Answers

Marcin Raczkowski

3/15/2007 2:07:00 PM

0

On Thursday 15 March 2007 13:57, Damjan Rems wrote:
> What I am trying to accomplist is:
>
> class MyClass
> def addVar(var, val)
> instance_variable_set("@#{var}", val)
> define_method("#{var}") { || instance_variable_get("#{var}") }
> end
> end
>
> c = MyClass.new
> c.addVar('aa',10) # here pops the error
> c.aa # should return 10
>
> But I get this error:
> NoMethodError: undefined method `define_method' for #<MyClass:0x2d01cc0
> @aa=10>
> from (irb):4:in `addVar'
> from (irb):8
>
> I know this should work because it's used all over rails.
>
> Advice me please
>
> TheR
define_method won't work in instance method, you have to use class method o
just use eval("def #{var}; here return value; end");

Damjan Rems

3/15/2007 2:18:00 PM

0

Marcin Raczkowski wrote:
> On Thursday 15 March 2007 13:57, Damjan Rems wrote:
>> c.addVar('aa',10) # here pops the error
>> Advice me please
>>
>> TheR
> define_method won't work in instance method, you have to use class
> method o
> just use eval("def #{var}; here return value; end");

Yep: Each of these two variants work:
eval "def #{var};instance_variable_get('@#{var}');end"
eval("def #{var}; @#{var}; end")

I was trying this too, but forgot to add @ to variable name.


Thanks

TheR


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

Brian Candler

3/16/2007 9:46:00 PM

0

On Thu, Mar 15, 2007 at 10:57:54PM +0900, Damjan Rems wrote:
> What I am trying to accomplist is:
>
> class MyClass
> def addVar(var, val)
> instance_variable_set("@#{var}", val)
> define_method("#{var}") { || instance_variable_get("#{var}") }

self.class.class_eval {
define_method("#{var}") { || instance_variable_get("@#{var}") }
}

> end
> end
>
> c = MyClass.new
> c.addVar('aa',10) # here pops the error
> c.aa # should return 10

Regards,

Brian.

gazelle

2/6/2011 11:46:00 AM

0

In article <iim0vr$dpa$2@reader1.panix.com>,
the wharf rat <wrat@panix.com> wrote:
>In article <4d9922cc-530e-43e3-a5bb-a2e5022f8d31@e21g2000yqe.googlegroups.com>,
>Tracey12 <tracey12email@gmail.com> wrote:
>>
>>EVERYONE KNOWS ITS THE UK, NOT FRANCE!!!
>
> Ummmm, remember the War of Independence? General Lafayette? Charles
>Lenfant? Rochambeau?
>
> What city was the treaty that ended the Revolutionary War signed in?

Cleveland, right?

--
Is God willing to prevent evil, but not able? Then he is not omnipotent.
Is he able, but not willing? Then he is malevolent.
Is he both able and willing? Then whence cometh evil?
Is he neither able nor willing? Then why call him God?
~ Epicurus

Pepe Le Jew

2/6/2011 5:35:00 PM

0

In article <iim0vr$dpa$2@reader1.panix.com>,
wrat@panix.com (the wharf rat) wrote:

> In article
> <4d9922cc-530e-43e3-a5bb-a2e5022f8d31@e21g2000yqe.googlegroups.com>,
> Tracey12 <tracey12email@gmail.com> wrote:
> >
> >EVERYONE KNOWS ITS THE UK, NOT FRANCE!!!
>
> Ummmm, remember the War of Independence? General Lafayette? Charles
> Lenfant? Rochambeau?
>
> What city was the treaty that ended the Revolutionary War signed in?

Miami Beach!

wy

2/6/2011 5:52:00 PM

0

On Feb 6, 6:45 am, Tracey12 <tracey12em...@gmail.com> wrote:
> On Feb 6, 5:34 am, w...@panix.com (the wharf rat) wrote:
>
> > In article <4d9922cc-530e-43e3-a5bb-a2e5022f8...@e21g2000yqe.googlegroups.com>,
>
> > Tracey12  <tracey12em...@gmail.com> wrote:
>
> > >EVERYONE KNOWS ITS THE UK, NOT FRANCE!!!
>
> >         Ummmm, remember the War of Independence?  General Lafayette? Charles
> > Lenfant? Rochambeau?
>
> >         What city was the treaty that ended the Revolutionary War signed in?
>
> The UK is our closest friend in all the world.;  France is no where
> near that.

Between the two, it can't be measured to any real discernible
difference, goofball.