[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Creating modules

benjohn

10/2/2006 8:46:00 AM


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.


17 Answers

David Vallner

10/2/2006 10:13:00 AM

0

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

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

benjohn

10/2/2006 10:25:00 AM

0

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



benjohn

10/2/2006 11:46:00 AM

0

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 Barnes

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


Luke Stark

10/2/2006 1:06:00 PM

0

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

but I'd like to do it dynamically.

Many thanks!

-L



Jano Svitok

10/2/2006 1:25:00 PM

0

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

Jean Helou

10/2/2006 1:32:00 PM

0

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

Jean Helou

10/2/2006 1:41:00 PM

0

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

Bruno Michel

10/2/2006 1:42:00 PM

0

Martin Coxall a écrit :
> 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

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

--
Bruno Michel

Luke Stark

10/2/2006 2:50:00 PM

0

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


Jean Helou

10/2/2006 2:59:00 PM

0

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