Creating modules

I am dynamically creating modules [1]. I would like to be able to add
methods and constants to the module using the “normal” notation:

module MyModule
def a_method; ‘wibble’; end
end

But the module doesn’t have an constant associated with it, so I’m
getting an error from…

class MyModuleClass < Module
def initialize(a_constant_to_go_in_module)
module self
K = a_constant_to_go_in_module
def a_method; end

end
end
end

Is there a way to do this, or do I have to start using the define_method
and const_set methods?

Cheers,
Benjohn

[1] These modules encapulate the behaviour of different types of
message. There are quite a few types of message; while all different,
they’re sufficiently similar that the differences can be described in a
table. It’s easy to think of a class of types of message: a given
message is an instance of a type of message.

[email protected] wrote:

end
end

Is there a way to do this, or do I have to start using the define_method
and const_set methods?

See David Black’s post in the “dynamic include” thread. The module
declaration creates a new independent scope, so you have to use the meta
methods.

David V.

David V.:

Is there a way to do this, or do I have to start using the
define_method
and const_set methods?

See David Black’s post in the “dynamic include” thread. The module
declaration creates a new independent scope, so you have to use the meta
methods.

Thanks, I’ll take a look…

I wrote before about the modules I’m trying to build on the fly. I’m
going to describe it a bit more and see if anyone has a thought about
what I should be doing here, because I’m not making much ground. [Using
modules was my latest foray in the direction of another approach, but I
think I’ve given up on that avenue]

What I have are messages and parameters. I’ll concentrate on parameters.

Parameters - I have instances of a parameter. Each parameter is an
instance of a type of parameter.

There are about 20 known types of parameter. I must also support unknown
types of parameter (which I will generate on the fly and furnish with
minimal generic behaviour). The known parameters fall in to two groups:
those that have a simple encoding and those with more complex encoding.
To sumarise, the types of parameter have a hierarchy:

ParameterType
UnknownParamerType < ParameterType
KnownParameterType < ParameterType
SimpleParameterType < KnownParameterType
ComplexParameterType < KnownParameterType

Remember that instances of these parameter types are not actual
parameters, they are the classes that parameters instances fall in to.

At the moment, I have a sepererate class for Parameter. Instances of
this delegate to a ParameterType instance. This is annoying me though:
it seems more complex than it ought to be.

I’ve tried quite a few approaches to putting all this together, but
nothing seems to fit the problem very well.

Any thoughts? Sorry if the above isn’t very clear!

Cheers,
Benjohn B.

p.s. I think this lends some support to prototype based information
models.

You may create singleton methods like so:

foo = MyThing.new

def foo.do_stuff
“code”
end

But I cannot seem to grock the syntax that would allow me to name the
method from a variable. Such as:

foo = MyThing.new
bar = “do_stuff”
def foo.%{#{bar}} #This is totally wrong I think.
“code”
end

Any suggestions? I’m starting to get a spinning head. :slight_smile:

To be clear, I want to create a singleton method on the instance of
MyThing, not add the method to the MyThing class.

This is mentioned here:

http://www.rubyist.net/~slagell/ruby/singletonmethods.html

but I’d like to do it dynamically.

Many thanks!

-L

On 10/2/06, Luke S. [email protected] wrote:

method from a variable. Such as:
MyThing, not add the method to the MyThing class.

This is mentioned here:

http://www.rubyist.net/~slagell/ruby/singletonmethods.html

but I’d like to do it dynamically.

Something like this:

obj.instance_eval do
define method(:methodName) do |*args|
# some shit here
end
end

Regards,

Martin

On 10/2/06, Luke S. [email protected] wrote:

This is mentioned here:

http://www.rubyist.net/~slagell/ruby/singletonmethods.html

but I’d like to do it dynamically.

Many thanks!

foo = Object.new

def foo.bar
“bar”
end

meth = “baz”
eval <<-EOF
def foo.#{meth} ; “#{meth}” ; end
EOF

meth = “bax”
foo.instance_eval <<-EOF
def #{meth} ; “#{meth}” ; end
EOF

puts foo.bar
puts foo.baz
puts foo.bax

class MyThing; end
f=MyThing.new
f.instance_eval do
class << self
define_method :test {puts “test”}
end
end

jean

On 10/2/06, Martin C. [email protected] wrote:

“code”

Something like this:

obj.instance_eval do
define method(:methodName) do |*args|
# some shit here
end
end

And, of course, that should be define_method().

Martin

On 10/2/06, Jan S. [email protected] wrote:

On 10/2/06, Luke S. [email protected] wrote:

You may create singleton methods like so:

I’ll have one more go. This time I’ll get it right. ;p

obj.class.instance_eval do
define_method(:method_name) do |*args|
# Method goes here
end
end

N.B. this actually works this time, because I tried it.

Regards,

Martin

However this will define the given method for all new instances of
obj.class (MyThing in my example) which are created after this code is
executed, which is not what the OP seemed to want.

jean

ps: in my example :test can be a variable and String.to_sym is your
friend.

Martin C. a écrit :

define_method(:method_name) do |*args|
# Method goes here
end
end

N.B. this actually works this time, because I tried it.

Regards,

Martin

Hi,

You can use class_eval, or better no *eval function :

$ cat my_thing.rb
#!/usr/bin/env ruby

class MyThing; end
foo = MyThing.new

class << foo
bar = “do_stuff”
define_method(bar) { “code” }
end

puts foo.do_stuff

$ ruby my_thing.rb
code

Jean,

Hmm. This looks close, but the :test in your example can only be a
variable that has been defined within the instance_eval…and I need to
pass one from outside.

The others run, but add the method to the class, rather than the
instance of that class.

I’m sure there’s a simple way. I just can’t see it.

-L

this seems to work

$ irb
irb(main):001:0> o=Object.new
=> #Object:0x100e3128
irb(main):002:0> s=“test”
=> “test”
irb(main):003:0> sym=s.to_sym
=> :test
irb(main):004:0> o.instance_eval do
irb(main):005:1* puts s
irb(main):006:1> klass=class<<self;self;end
irb(main):007:1> klass.send(:define_method,sym){puts “#{s}”}
irb(main):008:1> end
test
=> #Proc:[email protected]:7(irb)
irb(main):009:0> o.test
test
=> nil

On 2006.10.02 20:46, [email protected] wrote:

I wrote before about the modules I’m trying to build on the fly. I’m
going to describe it a bit more and see if anyone has a thought about
what I should be doing here, because I’m not making much ground. [Using
modules was my latest foray in the direction of another approach, but I
think I’ve given up on that avenue]

Yes, I think the problem is that this is way overcomplicated. Earlier
you were subclassing module which rarely makes sense.

ParameterType
UnknownParamerType < ParameterType
KnownParameterType < ParameterType
SimpleParameterType < KnownParameterType
ComplexParameterType < KnownParameterType

Remember that instances of these parameter types are not actual
parameters, they are the classes that parameters instances fall in to.

Does this mean that

type = SomeParameterType.new
parameter_of_type = type.new

–and if so, why? Classes are there for a reason :slight_smile:

Or do you mean that each Type contains metadata for parameters
of its type?

At the moment, I have a sepererate class for Parameter. Instances of
this delegate to a ParameterType instance. This is annoying me though:
it seems more complex than it ought to be.

I’ve tried quite a few approaches to putting all this together, but
nothing seems to fit the problem very well.

Any thoughts? Sorry if the above isn’t very clear!

I think your problem comes from trying to (if I understand correctly)
separate Parameters and ParameterTypes.

I do not pretend to understand the problem domain but I do think you
need
to go back to basics and just implement the system traditional OO. If a
parameter type has behaviour differing from its siblings, define it a
separate class.

class Parameter; end
class UnknownParameter < Parameter; end
class KnownParameter < Parameter; end
class SomeParameterType < KnownParameter; end

Aha, got a SomeParameter

my_param = SomeParameter.new arguments, go, here

Cheers,
Benjohn B.

p.s. I think this lends some support to prototype based information models.

Prototyping is based on cloning objects, not classes. If you were to
take this approach, you would simply a parameter object which exhibited
the correct behaviour for its type. Instantiating new parameter objects
would happen by #cloning the prototype and changing values as necessary.
The prototype object could be constructed either by instantiating a
class or then in the ‘true’ style by defining singleton methods on a
plain Object.

On Mon, Oct 02, 2006 at 05:45:52PM +0900, [email protected] wrote:

Is there a way to do this, or do I have to start using the define_method
and const_set methods?

No, well at least not if you know the method name before hand.

def initialize(a_constant_to_go_in_module)
self.module_eval do
K = a_constant_to_go_in_module
def a_method; ‘wibble’; end
end
end

getting an error from…

Is there a way to do this, or do I have to start using the
define_method
and const_set methods?

No, well at least not if you know the method name before hand.

I presume you meant “… not if you don’t know the method name …”?

def initialize(a_constant_to_go_in_module)
self.module_eval do
K = a_constant_to_go_in_module
def a_method; ‘wibble’; end
end
end

Ah! I think that’s exactly what I want :slight_smile: Thanks, I’ll give it a try :slight_smile:

Thank you Jean, that works wonderfully.

Thank you everyone else for your input as well.

-L

irb(main):001:0> o=Object.new
test

variable that has been defined within the instance_eval…and I need
to

From: Jean H. [mailto:[email protected]]
Sent: Monday, October 02, 2006 9:41 AM
To: ruby-talk ML
Subject: Re: Creating dynamically named singleton methods. Syntax
question.

However this will define the given method for all new instances of
obj.class (MyThing in my example) which are created after this
code is

executed, which is not what the OP seemed to want.

jean

ps: in my example :test can be a variable and String.to_sym is
your

Is there a way to do this, or do I have to start using the
define_method
and const_set methods?

No, well at least not if you know the method name before hand.

I presume you meant “… not if you don’t know the method name …”?

Ah - I get you - you were confirming the first part of my sentence, and
rejecting the latter. :slight_smile: Right! Thanks.

you were subclassing module which rarely makes sense.
:slight_smile: I had a try at sub classing Class for a while (I’m still not sure if
that actually makes sense, I think it does), but Ruby doesn’t seem to
like that:

irb(main):001:0> class ParameterClass < Class
irb(main):002:1> end
TypeError: can’t make subclass of Class
from (irb):1

snip

Does this mean that

type = SomeParameterType.new
parameter_of_type = type.new

–and if so, why? Classes are there for a reason :slight_smile:

Yes, that’s what I mean. There are a large number of parameter types. I
don’t want to describe all of them individually as classes; in fact, I’m
unable to describe them all individually because some of the types of
parameter will not be known about until run time.

I know there are a lot of ways of skinning this. I know that I could
build up each parameter individually, and forget about trying to
describe the classes they have in common (even though I don’t want to
describe them all individually)…

The current solution has objects representing the different types of
parameter, which is a start. As those objects are conceptually identical
to classes though (in my brain anyway), I’d like them to actually be
classes. I feel that doing so would be concordant with Ruby’s object
model, and should lead to an elegant solution.

Or do you mean that each Type contains metadata for parameters
of its type?

:slight_smile: Metadata?

The parameters fall in to a large number of different parameter types.
There is common behaviour between each parameter of a given type. Some
of the parameter types I want to define (but very briefly, without
needing to do…:

class yet_another_parameter;

end

…and some I may encounter “on the fly”. The types themselves fall in
to a small number of seperate parameter type types (yuck).

snip

I also need the parameter_types to exist as objects in their own right;
I need to be able to pass them about them in the program, store them in
tables, and look them up.

p.s. I think this lends some support to prototype based information
models.

Prototyping is based on cloning objects, not classes. If you were to
take this approach, you would simply a parameter object which exhibited
the correct behaviour for its type. Instantiating new parameter objects
would happen by #cloning the prototype and changing values as necessary.
The prototype object could be constructed either by instantiating a
class or then in the ‘true’ style by defining singleton methods on a
plain Object.

nods I think that aligns with what I understood of prototype systems.
The reason I lean towards that approach is because, having done away
with classes, you don’t need to decide if something is a class or not. I
would imagine that having a single uniform concept would be a lot
easier. You don’t need to think through whether something is a lot like
a class, but isn’t really a class because…

However, another post in this thread has given me the hope that if I
have another try at this, I’ll be able to come up with a good solution
:slight_smile:

Thanks for the help,
Cheers,
Benj

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs