# Creating modules

**URL:** <https://rubytalk.org/t/creating-modules/31524>\
**Category:** ruby-talk\
**Created:** [2 October 2006 08:45 UTC](https://rubytalk.org/t/creating-modules/31524 "2006-10-02T08:45:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjohn\_Barnes](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@Benjohn\_Barnes](https://rubytalk.org/u/Benjohn_Barnes)\
**Post date:** [2 October 2006 08:45 UTC](https://rubytalk.org/t/creating-modules/31524/1 "2006-10-02T08:45:52Z")

</div>

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  
&nbsp;&nbsp;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  
&nbsp;&nbsp;def initialize(a\_constant\_to\_go\_in\_module)  
&nbsp;&nbsp;&nbsp;&nbsp;module self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;K = a\_constant\_to\_go\_in\_module  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def a\_method; end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

Is there a way to do this, or do I have to start using the define\_method  
and const\_set methods?

Cheers,  
&nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![David\_Vallner](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Vallner](https://rubytalk.org/u/David_Vallner)\
**Post date:** [2 October 2006 10:12 UTC](https://rubytalk.org/t/creating-modules/31524/2 "2006-10-02T10:12:52Z")

</div>

benjohn@fysh.org wrote:

> But the module doesn't have an constant associated with it, so I'm  
> getting an error from...
> 
> class MyModuleClass \< Module  
> &nbsp;&nbsp;def initialize(a\_constant\_to\_go\_in\_module)  
> &nbsp;&nbsp;&nbsp;&nbsp;module self  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;K = a\_constant\_to\_go\_in\_module  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def a\_method; end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;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 Vallner

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [2 October 2006 17:07 UTC](https://rubytalk.org/t/creating-modules/31524/3 "2006-10-02T17:07:11Z")

</div>

> 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  
> &nbsp;&nbsp;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  
> &nbsp;&nbsp;def initialize(a\_constant\_to\_go\_in\_module)  
> &nbsp;&nbsp;&nbsp;&nbsp;module self  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;K = a\_constant\_to\_go\_in\_module  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def a\_method; end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end  
> end
> 
> 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)  
&nbsp;&nbsp;self.module\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;K = a\_constant\_to\_go\_in\_module  
&nbsp;&nbsp;&nbsp;&nbsp;def a\_method; 'wibble'; end  
&nbsp;&nbsp;end  
end

> **···**
>
> On Mon, Oct 02, 2006 at 05:45:52PM +0900, benjohn@fysh.org wrote:
> 
> > Cheers,  
> > &nbsp;&nbsp;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.

---

<div class="post-metadata">

**Author:** ![Benjohn\_Barnes](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@Benjohn\_Barnes](https://rubytalk.org/u/Benjohn_Barnes)\
**Post date:** [2 October 2006 10:25 UTC](https://rubytalk.org/t/creating-modules/31524/4 "2006-10-02T10:25:06Z")

</div>

David Vallner:

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

---

<div class="post-metadata">

**Author:** ![Benjohn\_Barnes](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@Benjohn\_Barnes](https://rubytalk.org/u/Benjohn_Barnes)\
**Post date:** [2 October 2006 11:46 UTC](https://rubytalk.org/t/creating-modules/31524/5 "2006-10-02T11:46:26Z")

</div>

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,  
&nbsp;&nbsp;Benjohn Barnes

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

---

<div class="post-metadata">

**Author:** ![Benjohn\_Barnes](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@Benjohn\_Barnes](https://rubytalk.org/u/Benjohn_Barnes)\
**Post date:** [3 October 2006 07:22 UTC](https://rubytalk.org/t/creating-modules/31524/6 "2006-10-03T07:22:59Z")

</div>

> > 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  
> > &nbsp;&nbsp;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  
> > &nbsp;&nbsp;def initialize(a\_constant\_to\_go\_in\_module)  
> > &nbsp;&nbsp;&nbsp;&nbsp;module self  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;K = a\_constant\_to\_go\_in\_module  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def a\_method; end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;end  
> > end
> > 
> > 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)  
> &nbsp;&nbsp;self.module\_eval do  
> &nbsp;&nbsp;&nbsp;&nbsp;K = a\_constant\_to\_go\_in\_module  
> &nbsp;&nbsp;&nbsp;&nbsp;def a\_method; 'wibble'; end  
> &nbsp;&nbsp;end  
> end

Ah! I think that's exactly what I want 🙂 Thanks, I'll give it a try 🙂

> **···**
>
> > On Mon, Oct 02, 2006 at 05:45:52PM +0900, benjohn@fysh.org wrote:

---

<div class="post-metadata">

**Author:** ![Luke\_Stark](https://avatars.discourse-cdn.com/v4/letter/l/ba9def/32.png) [@Luke\_Stark](https://rubytalk.org/u/Luke_Stark)\
**Post date:** [2 October 2006 13:05 UTC](https://rubytalk.org/t/creating-modules/31524/7 "2006-10-02T13:05:42Z")

</div>

You may create singleton methods like so:

foo = MyThing.new

def foo.do\_stuff  
&nbsp;&nbsp;"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.  
&nbsp;&nbsp;"code"  
end

Any suggestions? I'm starting to get a spinning head. 🙂

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](http://www.rubyist.net/~slagell/ruby/singletonmethods.html)

but I'd like to do it dynamically.

Many thanks!

-L

---

<div class="post-metadata">

**Author:** ![Eero\_Saynatkari2](https://avatars.discourse-cdn.com/v4/letter/e/ecae2f/32.png) [@Eero\_Saynatkari2](https://rubytalk.org/u/Eero_Saynatkari2)\
**Post date:** [2 October 2006 15:23 UTC](https://rubytalk.org/t/creating-modules/31524/8 "2006-10-02T15:23:42Z")

</div>

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

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

Does this mean that

&nbsp;&nbsp;&nbsp;type = SomeParameterType.new  
&nbsp;&nbsp;&nbsp;parameter\_of\_type = type.new

--and if so, why? Classes are there for a reason 🙂

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.

&nbsp;&nbsp;class Parameter; end  
&nbsp;&nbsp;class UnknownParameter \< Parameter; end  
&nbsp;&nbsp;class KnownParameter \< Parameter; end  
&nbsp;&nbsp;class SomeParameterType \< KnownParameter; end

&nbsp;&nbsp;# Aha, got a SomeParameter  
&nbsp;&nbsp;my\_param = SomeParameter.new arguments, go, here

> Cheers,  
> &nbsp;&nbsp;Benjohn Barnes
> 
> 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 2006.10.02 20:46, benjohn@fysh.org wrote:

---

<div class="post-metadata">

**Author:** ![Benjohn\_Barnes](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@Benjohn\_Barnes](https://rubytalk.org/u/Benjohn_Barnes)\
**Post date:** [3 October 2006 07:56 UTC](https://rubytalk.org/t/creating-modules/31524/9 "2006-10-03T07:56:32Z")

</div>

> > > 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. 🙂 Right! Thanks.

---

<div class="post-metadata">

**Author:** ![Benjohn\_Barnes](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@Benjohn\_Barnes](https://rubytalk.org/u/Benjohn_Barnes)\
**Post date:** [3 October 2006 08:30 UTC](https://rubytalk.org/t/creating-modules/31524/10 "2006-10-03T08:30:56Z")

</div>

> > def initialize(a\_constant\_to\_go\_in\_module)  
> > &nbsp;&nbsp;self.module\_eval do  
> > &nbsp;&nbsp;&nbsp;&nbsp;K = a\_constant\_to\_go\_in\_module  
> > &nbsp;&nbsp;&nbsp;&nbsp;def a\_method; 'wibble'; end  
> > &nbsp;&nbsp;end  
> > end
> 
> Ah! I think that's exactly what I want 🙂 Thanks, I'll give it a try 🙂

So, I've been having an experiment, and here's what I've found so far...

irb(main):001:0\> class MyModule \< Module  
irb(main):002:1\> def initialize(k)  
irb(main):003:2\> module\_eval {K=k}  
irb(main):004:2\> end  
irb(main):005:1\> end  
SyntaxError: compile error  
(irb):3: dynamic constant assignment  
module\_eval {K=k}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):5

But I can do...

irb(main):001:0\> x=10  
=\> 10  
irb(main):002:0\> m=Module.new  
=\> #\<Module:0xb73ee938\>  
irb(main):003:0\> m.module\_eval {K=x}  
=\> 10

However, if I then try...

irb(main):004:0\> m::K  
NameError: uninitialized constant #\<Module:0xb73ee938\>::K  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):4  
irb(main):005:0\> m.constants  
=\>

But I can do...

irb(main):006:0\> m.const\_get(:X)  
NameError: uninitialized constant #\<Module:0xb73ee938\>::X  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6:in `const\_get'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6  
irb(main):007:0\> m.const\_get(:K)  
=\> 10

!? Huh? I guess this is something to do with constants being looked up  
in a global symbol pool?

Going back to trying to define a sub class of modules...

irb(main):009:0\> vishnu in new\_try# irb  
irb(main):001:0\> class MyModule \< Module  
irb(main):002:1\> def initialize(x)  
irb(main):003:2\> super()  
irb(main):004:2\> module\_eval do  
irb(main):005:3\* def f; x; end  
irb(main):006:3\> def self.g; x\*x; end  
irb(main):007:3\> end  
irb(main):008:2\> end  
irb(main):009:1\> end  
=\> nil  
irb(main):010:0\> m=MyModule.new(10)  
=\> #\<MyModule:0xb73ded44\>  
irb(main):011:0\> m.g  
NameError: undefined local variable or method `x' for  
#\<MyModule:0xb73ded44\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):6:in `g'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):11  
irb(main):012:0\> Object.new.extend(m).f  
NameError: undefined local variable or method `x' for #\<Object:0xb73d6270\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):5:in `f'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):12  
irb(main):013:0\>

...Which is a bit of a pitty.

I also briefly dabled with...

irb(main):018:0\> class ParameterClass \< Class  
irb(main):019:1\> end  
TypeError: can't make subclass of Class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):18  
irb(main):020:0\>

And turned away from that avenue.

I think I need a better idea of what is and isn't possible here. I think  
my understanding of the dark secrets is very little; I was hoping I'd  
get away with that though, as I generally have with Ruby until now 🙂

Cheers,  
&nbsp;&nbsp;Benjohn

---

<div class="post-metadata">

**Author:** ![Martin\_Coxall1](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@Martin\_Coxall1](https://rubytalk.org/u/Martin_Coxall1)\
**Post date:** [2 October 2006 13:14 UTC](https://rubytalk.org/t/creating-modules/31524/11 "2006-10-02T13:14:49Z")

</div>

Something like this:

obj.instance\_eval do  
&nbsp;&nbsp;&nbsp;define method(:methodName) do |\*args|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# some shit here  
&nbsp;&nbsp;&nbsp;end  
end

Regards,

Martin

> **···**
>
> On 10/2/06, Luke Stark \<Luke.Stark@mathworks.com\> wrote:
> 
> > You may create singleton methods like so:
> > 
> > foo = MyThing.new
> > 
> > def foo.do\_stuff  
> > &nbsp;&nbsp;"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.  
> > &nbsp;&nbsp;"code"  
> > end
> > 
> > Any suggestions? I'm starting to get a spinning head. 🙂
> > 
> > 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](http://www.rubyist.net/~slagell/ruby/singletonmethods.html)
> > 
> > but I'd like to do it dynamically.

---

<div class="post-metadata">

**Author:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [2 October 2006 13:25 UTC](https://rubytalk.org/t/creating-modules/31524/12 "2006-10-02T13:25:06Z")

</div>

foo = Object.new

def foo.bar  
&nbsp;&nbsp;"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

> **···**
>
> On 10/2/06, Luke Stark \<Luke.Stark@mathworks.com\> wrote:
> 
> > You may create singleton methods like so:
> > 
> > foo = MyThing.new
> > 
> > def foo.do\_stuff  
> > &nbsp;&nbsp;"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.  
> > &nbsp;&nbsp;"code"  
> > end
> > 
> > Any suggestions? I'm starting to get a spinning head. 🙂
> > 
> > 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](http://www.rubyist.net/~slagell/ruby/singletonmethods.html)
> > 
> > but I'd like to do it dynamically.
> > 
> > Many thanks!

---

<div class="post-metadata">

**Author:** ![Benjohn\_Barnes](https://avatars.discourse-cdn.com/v4/letter/b/df705f/32.png) [@Benjohn\_Barnes](https://rubytalk.org/u/Benjohn_Barnes)\
**Post date:** [3 October 2006 07:53 UTC](https://rubytalk.org/t/creating-modules/31524/13 "2006-10-03T07:53:24Z")

</div>

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

🙂 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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1

\*snip\*

> Does this mean that
> 
> &nbsp;&nbsp;&nbsp;type = SomeParameterType.new  
> &nbsp;&nbsp;&nbsp;parameter\_of\_type = type.new
> 
> --and if so, why? Classes are there for a reason 🙂

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?

🙂 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  
🙂

Thanks for the help,  
&nbsp;&nbsp;Cheers,  
&nbsp;&nbsp;&nbsp;&nbsp;Benj

> **···**
>
> > On 2006.10.02 20:46, benjohn@fysh.org wrote:

---

<div class="post-metadata">

**Author:** ![Martin\_Coxall1](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@Martin\_Coxall1](https://rubytalk.org/u/Martin_Coxall1)\
**Post date:** [2 October 2006 13:19 UTC](https://rubytalk.org/t/creating-modules/31524/14 "2006-10-02T13:19:04Z")

</div>

And, of course, that should be define\_method().

Martin

> **···**
>
> On 10/2/06, Martin Coxall \<pseudo.meta@gmail.com\> wrote:
> 
> > On 10/2/06, Luke Stark \<Luke.Stark@mathworks.com\> wrote:  
> > \>  
> > \> 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. 🙂  
> > \>  
> > \> 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&lt;http://www.rubyist.net/~slagell/ruby/singletonmethods.html&gt](http://www.rubyist.net/~slagell/ruby/singletonmethods.html&lt;http://www.rubyist.net/%7Eslagell/ruby/singletonmethods.html&gt);  
> > \>  
> > \> but I'd like to do it dynamically.  
> > \>
> > 
> > Something like this:
> > 
> > obj.instance\_eval do  
> > &nbsp;&nbsp;&nbsp;define method(:methodName) do |\*args|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# some shit here  
> > &nbsp;&nbsp;&nbsp;end  
> > end

---

<div class="post-metadata">

**Author:** ![Jean\_Helou](https://avatars.discourse-cdn.com/v4/letter/j/a9adbd/32.png) [@Jean\_Helou](https://rubytalk.org/u/Jean_Helou)\
**Post date:** [2 October 2006 13:32 UTC](https://rubytalk.org/t/creating-modules/31524/15 "2006-10-02T13:32:03Z")

</div>

class MyThing; end  
f=MyThing.new  
f.instance\_eval do  
&nbsp;&nbsp;class \<\< self  
&nbsp;&nbsp;&nbsp;&nbsp;define\_method :test {puts "test"}  
&nbsp;&nbsp;end  
end

jean

> **···**
>
> On 10/2/06, Martin Coxall \<pseudo.meta@gmail.com\> wrote:
> 
> > On 10/2/06, Luke Stark \<Luke.Stark@mathworks.com\> wrote:  
> > \>  
> > \> 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. 🙂  
> > \>  
> > \> 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](http://www.rubyist.net/~slagell/ruby/singletonmethods.html)  
> > \>  
> > \> but I'd like to do it dynamically.  
> > \>
> > 
> > Something like this:
> > 
> > obj.instance\_eval do  
> > &nbsp;&nbsp;&nbsp;define method(:methodName) do |\*args|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# some shit here  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > Regards,
> > 
> > Martin

---

<div class="post-metadata">

**Author:** ![Martin\_Coxall1](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@Martin\_Coxall1](https://rubytalk.org/u/Martin_Coxall1)\
**Post date:** [2 October 2006 13:36 UTC](https://rubytalk.org/t/creating-modules/31524/16 "2006-10-02T13:36:13Z")

</div>

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

obj.class.instance\_eval do  
&nbsp;&nbsp;&nbsp;define\_method(:method\_name) do |\*args|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Method goes here  
&nbsp;&nbsp;&nbsp;end  
end

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

Regards,

Martin

> **···**
>
> On 10/2/06, Jan Svitok \<jan.svitok@gmail.com\> wrote:
> 
> > On 10/2/06, Luke Stark \<Luke.Stark@mathworks.com\> wrote:  
> > \> You may create singleton methods like so:  
> > \>

---

<div class="post-metadata">

**Author:** ![Jean\_Helou](https://avatars.discourse-cdn.com/v4/letter/j/a9adbd/32.png) [@Jean\_Helou](https://rubytalk.org/u/Jean_Helou)\
**Post date:** [2 October 2006 13:41 UTC](https://rubytalk.org/t/creating-modules/31524/17 "2006-10-02T13:41:18Z")

</div>

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.

> **···**
>
> On 10/2/06, Martin Coxall \<pseudo.meta@gmail.com\> wrote:
> 
> > On 10/2/06, Jan Svitok \<jan.svitok@gmail.com\> wrote:  
> > \>  
> > \> On 10/2/06, Luke Stark \<Luke.Stark@mathworks.com\> 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  
> > &nbsp;&nbsp;&nbsp;define\_method(:method\_name) do |\*args|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Method goes here  
> > &nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > N.B. this actually works this time, because I tried it.
> > 
> > Regards,
> > 
> > Martin

---

<div class="post-metadata">

**Author:** ![Bruno\_Michel](https://avatars.discourse-cdn.com/v4/letter/b/7cd45c/32.png) [@Bruno\_Michel](https://rubytalk.org/u/Bruno_Michel)\
**Post date:** [2 October 2006 13:42 UTC](https://rubytalk.org/t/creating-modules/31524/18 "2006-10-02T13:42:19Z")

</div>

Martin Coxall a écrit :

> > \> 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  
> &nbsp;&nbsp;define\_method(:method\_name) do |\*args|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Method goes here  
> &nbsp;&nbsp;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  
&nbsp;&nbsp;&nbsp;bar = "do\_stuff"  
&nbsp;&nbsp;&nbsp;define\_method(bar) { "code" }  
end

puts foo.do\_stuff

$ ruby my\_thing.rb  
code

> **···**
>
> > On 10/2/06, Jan Svitok \<jan.svitok@gmail.com\> wrote:
> > 
> > > On 10/2/06, Luke Stark \<Luke.Stark@mathworks.com\> wrote:
> 
> --  
> Bruno Michel

---

<div class="post-metadata">

**Author:** ![Luke\_Stark](https://avatars.discourse-cdn.com/v4/letter/l/ba9def/32.png) [@Luke\_Stark](https://rubytalk.org/u/Luke_Stark)\
**Post date:** [2 October 2006 14:49 UTC](https://rubytalk.org/t/creating-modules/31524/19 "2006-10-02T14:49:57Z")

</div>

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

> **···**
>
> > -----Original Message-----  
> > From: Jean Helou [mailto:jean.helou@gmail.com]  
> > 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  
> > friend.
> > 
> > On 10/2/06, Martin Coxall \<pseudo.meta@gmail.com\> wrote:  
> > \> On 10/2/06, Jan Svitok \<jan.svitok@gmail.com\> wrote:  
> > \> \>  
> > \> \> On 10/2/06, Luke Stark \<Luke.Stark@mathworks.com\> 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  
> > \>  
> > \>

---

<div class="post-metadata">

**Author:** ![Jean\_Helou](https://avatars.discourse-cdn.com/v4/letter/j/a9adbd/32.png) [@Jean\_Helou](https://rubytalk.org/u/Jean_Helou)\
**Post date:** [2 October 2006 14:59 UTC](https://rubytalk.org/t/creating-modules/31524/20 "2006-10-02T14:59:28Z")

</div>

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:0x003df7d8@(irb):7\>  
irb(main):009:0\> o.test  
test  
=\> nil

> **···**
>
> On 10/2/06, Luke Stark \<Luke.Stark@mathworks.com\> wrote:
> 
> > 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
> > 
> > \> -----Original Message-----  
> > \> From: Jean Helou [mailto:jean.helou@gmail.com]  
> > \> 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  
> > \> friend.  
> > \>  
> > \> On 10/2/06, Martin Coxall \<pseudo.meta@gmail.com\> wrote:  
> > \> \> On 10/2/06, Jan Svitok \<jan.svitok@gmail.com\> wrote:  
> > \> \> \>  
> > \> \> \> On 10/2/06, Luke Stark \<Luke.Stark@mathworks.com\> 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  
> > \> \>

[Next page](https://rubytalk.org/t/creating-modules/31524.md?page=2)
