PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > define_method
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
define_method

Réponse
 
LinkBack Outils de la discussion
Vieux 07/11/2007, 04h37   #1
Malcolm Lockyer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut define_method

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 appreciated!

Thanks,
Malcolm.

  Réponse avec citation
Vieux 07/11/2007, 07h07   #2
Trans
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: define_method



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


  Réponse avec citation
Vieux 07/11/2007, 07h15   #3
Jacob Basham
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: define_method

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 appreciated!
>
> Thanks,
> Malcolm.
>



  Réponse avec citation
Vieux 07/11/2007, 12h41   #4
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: define_method

2007/11/7, Malcolm Lockyer <maxpenguin@gmail.com>:
> 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

  Réponse avec citation
Vieux 07/11/2007, 21h19   #5
Malcolm Lockyer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: define_method

Hey, thanks for the replies guys, I am pretty noob with ruby so thanks
for your .

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 guys.
Malcolm

On 11/8/07, Malcolm Lockyer <malcolm@endev.co.nz> wrote:
> Hey, thanks for the replies guys, I am pretty noob with ruby so thanks
> for your .
>
> 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 guys.
> Malcolm
>
> On 11/8/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> > 2007/11/7, Malcolm Lockyer <maxpenguin@gmail.com>:
> > > 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
> >
> >

>


  Réponse avec citation
Vieux 08/11/2007, 07h00   #6
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: define_method


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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h02.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16233 seconds with 14 queries