Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > define_method

Reply
Thread Tools

define_method

 
 
Malcolm Lockyer
Guest
Posts: n/a
 
      11-07-2007
Hi Guys,

I am trying to use define_method like:

class TestClass
def initialize
define_method("hello") { || puts "Hello" }
end
end

x = TestClass.new
x.hello

I am always getting:
NoMethodError: undefined method `define_method'

Do I need to require something? Am I doing something wrong, or am I
completely missing the boat!?


Any help appreciated!

Thanks,
Malcolm.

 
Reply With Quote
 
 
 
 
Trans
Guest
Posts: n/a
 
      11-07-2007


On Nov 6, 11:37 pm, "Malcolm Lockyer" <maxpeng...@gmail.com> wrote:
> Hi Guys,
>
> I am trying to use define_method like:


def selfclass
(class << self; self; end)
end

class TestClass
def initialize
selfclass.send(:define_method, "hello") { || puts "Hello" }
end
end

x = TestClass.new
x.hello


 
Reply With Quote
 
 
 
 
Jacob Basham
Guest
Posts: n/a
 
      11-07-2007
You are getting this error since define_method is defined in Module
not Object. Your object needs to call define_method against it's Class
definition, not self.

Here's an example how to use it.
http://www.ruby-doc.org/core/classes...e.html#M001677

Best,
Jake

On Nov 6, 2007, at 10:37 PM, Malcolm Lockyer wrote:

> Hi Guys,
>
> I am trying to use define_method like:
>
> class TestClass
> def initialize
> define_method("hello") { || puts "Hello" }
> end
> end
>
> x = TestClass.new
> x.hello
>
> I am always getting:
> NoMethodError: undefined method `define_method'
>
> Do I need to require something? Am I doing something wrong, or am I
> completely missing the boat!?
>
>
> Any help appreciated!
>
> Thanks,
> Malcolm.
>



 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-07-2007
2007/11/7, Malcolm Lockyer <>:
> Hi Guys,
>
> I am trying to use define_method like:
>
> class TestClass
> def initialize
> define_method("hello") { || puts "Hello" }
> end
> end
>
> x = TestClass.new
> x.hello
>
> I am always getting:
> NoMethodError: undefined method `define_method'
>
> Do I need to require something? Am I doing something wrong, or am I
> completely missing the boat!?


Is this really your use case? Since you know which method you define,
you can do it with a normal def anyway. So I suspect that you are
doing something else. What exactly is it?

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

 
Reply With Quote
 
Malcolm Lockyer
Guest
Posts: n/a
 
      11-07-2007
Hey, thanks for the replies guys, I am pretty noob with ruby so thanks
for your help.

Yes Robert, that wasn't my use case - that was just the most
simplified code that shows the problem I was having. I didn't want to
bore you all with loads of irrelevant code. If you are interested I
was trying to build a dynamic enumeration style class (vaguely similar
to java or c#'s way of doing it), where I could have the pseudo
constant (i.e. TestClass.Blue, TestClass.Red etc.) setup by calling
the constructor something like TestClass.new(:Blue => 1, :Red => 2
etc.) and also be able to iterate through the keys and stuff.


Anyway, thanks again for your help guys.
Malcolm

On 11/8/07, Malcolm Lockyer <> wrote:
> Hey, thanks for the replies guys, I am pretty noob with ruby so thanks
> for your help.
>
> Yes Robert, that wasn't my use case - that was just the most
> simplified code that shows the problem I was having. I didn't want to
> bore you all with loads of irrelevant code. If you are interested I
> was trying to build a dynamic enumeration style class (vaguely similar
> to java or c#'s way of doing it), where I could have the pseudo
> constant (i.e. TestClass.Blue, TestClass.Red etc.) setup by calling
> the constructor something like TestClass.new(:Blue => 1, :Red => 2
> etc.) and also be able to iterate through the keys and stuff.
>
>
> Anyway, thanks again for your help guys.
> Malcolm
>
> On 11/8/07, Robert Klemme <> wrote:
> > 2007/11/7, Malcolm Lockyer <>:
> > > Hi Guys,
> > >
> > > I am trying to use define_method like:
> > >
> > > class TestClass
> > > def initialize
> > > define_method("hello") { || puts "Hello" }
> > > end
> > > end
> > >
> > > x = TestClass.new
> > > x.hello
> > >
> > > I am always getting:
> > > NoMethodError: undefined method `define_method'
> > >
> > > Do I need to require something? Am I doing something wrong, or am I
> > > completely missing the boat!?

> >
> > Is this really your use case? Since you know which method you define,
> > you can do it with a normal def anyway. So I suspect that you are
> > doing something else. What exactly is it?
> >
> > Kind regards
> >
> > robert
> >
> > --
> > use.inject do |as, often| as.you_can - without end
> >
> >

>


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      11-08-2007

Please do not top post.

On 07.11.2007 22:19, Malcolm Lockyer wrote:
> Yes Robert, that wasn't my use case - that was just the most
> simplified code that shows the problem I was having. I didn't want to
> bore you all with loads of irrelevant code. If you are interested I
> was trying to build a dynamic enumeration style class (vaguely similar
> to java or c#'s way of doing it), where I could have the pseudo
> constant (i.e. TestClass.Blue, TestClass.Red etc.) setup by calling
> the constructor something like TestClass.new(:Blue => 1, :Red => 2
> etc.) and also be able to iterate through the keys and stuff.


In case you are not doing it for the fun of it and / or are looking for
more inspirations this is a good place to look:

http://raa.ruby-lang.org/search.rhtml?search=enum

Kind regards

robert
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
define_method to add a Class method? walter@mwsewall.com Ruby 9 07-03-2004 12:32 PM
define_method with singleton classes? Csaba Henk Ruby 5 06-16-2004 01:54 PM
define_method and blocks Florian G. Pflug Ruby 4 04-06-2004 12:57 PM
define_method with instance assignment to proc T. Onoma Ruby 2 12-06-2003 04:03 PM
arity of methods defined with define_method Paul Brannan Ruby 2 11-27-2003 05:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57