define_method

  • Thread starter Malcolm Lockyer
  • Start date
M

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


Any help appreciated!

Thanks,
Malcolm.
 
T

Trans

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

Robert Klemme

2007/11/7 said:
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
 
M

Malcolm Lockyer

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

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
 
R

Robert Klemme

Please do not top post.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top