[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Can methods be part of a array

Dikshit Saumya-G19445

8/23/2006 4:34:00 PM

Hi
I am a newbie to this mailing list.

I have a couple of queries:

1. Can methods be part of an array definition, which can bear similarity
to 'C' definitions
of "array of function pointers", as part of a state machine
implementation.

2. How can we make C routine call a "method" defined in a ruby script as
we do it the other way while extending ruby call C routines

Regards,
Saumya.


5 Answers

Jano Svitok

8/23/2006 4:44:00 PM

0

On 8/23/06, Dikshit Saumya-G19445 <somc@motorola.com> wrote:
> Hi
> I am a newbie to this mailing list.
>
> I have a couple of queries:
>
> 1. Can methods be part of an array definition, which can bear similarity
> to 'C' definitions
> of "array of function pointers", as part of a state machine
> implementation.

Look up Object#method and Method in the docs:

you can do ['aaa'.method(:size), 10.method("+")]

UnboundMethod could be useful as well.

Other than that, its possible to call methods by name. I you just need
to call methods on one object, it is enough to store just their names
(preferably as symbols):

methods = [:size, :length, :whatever]

then you would for example do: methods.each {|m| 'aaaa'.send(m) }

Rick DeNatale

8/23/2006 5:11:00 PM

0

On 8/23/06, Jan Svitok <jan.svitok@gmail.com> wrote:
> On 8/23/06, Dikshit Saumya-G19445 <somc@motorola.com> wrote:
> > Hi
> > I am a newbie to this mailing list.
> >
> > I have a couple of queries:> >
> > 1. Can methods be part of an array definition, which can bear similarity
> > to 'C' definitions
> > of "array of function pointers", as part of a state machine
> > implementation.
>
> Look up Object#method and Method in the docs:
>
> you can do ['aaa'.method(:size), 10.method("+")]
>
> UnboundMethod could be useful as well.
>
> Other than that, its possible to call methods by name. I you just need
> to call methods on one object, it is enough to store just their names
> (preferably as symbols):
>
> methods = [:size, :length, :whatever]
>
> then you would for example do: methods.each {|m| 'aaaa'.send(m) }
>
>


Another approach would be to use Procs in the array instead of
methods, this would allow you to store arguments in the array entries
as well, and to execute more than just a method.

actions = [
lambda {obj.size},
lambda {obj.length},
lambda {obj.foo(3, "abc") }
lambda {puts "Hi Mom!"; obj.callhome }
]

then to invoke an action it's just

actions[n].call

as a variation on this, if the blocks have parameters, you can use
call(arg1, [arg2,...]) to pass parameters.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Dikshit Saumya-G19445

8/24/2006 6:52:00 AM

0

Thanks
Making it a proc really helped.

I have few more problems

How can we make C routine call a "method" defined in a ruby script ?
I am familiar with extending ruby to call C routines with/without swig.
But I was not able to find a easy solution for it. The only way it may
workout is
spawning the script in a different process/thread from C code and then
make it do all query/answer sequence by calling a C routine. Is there a
easy way out?
Can't we make a C routine call a specific method in a Ruby Script.

Can we access a C stack from Ruby or vice versa while debugging the
Ruby extension.

Regards,
Saumya.


-----Original Message-----
From: Rick DeNatale [mailto:rick.denatale@gmail.com]
Sent: Wednesday, August 23, 2006 10:41 PM
To: ruby-talk ML
Subject: Re: Can methods be part of a array

On 8/23/06, Jan Svitok <jan.svitok@gmail.com> wrote:
> On 8/23/06, Dikshit Saumya-G19445 <somc@motorola.com> wrote:
> > Hi
> > I am a newbie to this mailing list.
> >
> > I have a couple of queries:> >
> > 1. Can methods be part of an array definition, which can bear
> > similarity to 'C' definitions
> > of "array of function pointers", as part of a state machine
> > implementation.
>
> Look up Object#method and Method in the docs:
>
> you can do ['aaa'.method(:size), 10.method("+")]
>
> UnboundMethod could be useful as well.
>
> Other than that, its possible to call methods by name. I you just need

> to call methods on one object, it is enough to store just their names
> (preferably as symbols):
>
> methods = [:size, :length, :whatever]
>
> then you would for example do: methods.each {|m| 'aaaa'.send(m) }
>
>


Another approach would be to use Procs in the array instead of methods,
this would allow you to store arguments in the array entries as well,
and to execute more than just a method.

actions = [
lambda {obj.size},
lambda {obj.length},
lambda {obj.foo(3, "abc") }
lambda {puts "Hi Mom!"; obj.callhome } ]

then to invoke an action it's just

actions[n].call

as a variation on this, if the blocks have parameters, you can use
call(arg1, [arg2,...]) to pass parameters.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...


Dikshit Saumya-G19445

8/24/2006 11:13:00 AM

0




I have few more problems

How can we make C routine call a "method" defined in a ruby script ?
I am familiar with extending ruby to call C routines with/without swig.
But I was not able to find a easy solution for it. The only way it may
workout is spawning the script in a different process/thread from C code
and then make it do all query/answer sequence by calling a C routine. Is
there a easy way out?
Can't we make a C routine call a specific method in a Ruby Script.

Can we access a C stack from Ruby or vice versa while debugging the
Ruby extension.

Regards,
Saumya.


-----Original Message-----
From: Rick DeNatale [mailto:rick.denatale@gmail.com]
Sent: Wednesday, August 23, 2006 10:41 PM
To: ruby-talk ML
Subject: Re: Can methods be part of a array

On 8/23/06, Jan Svitok <jan.svitok@gmail.com> wrote:
> On 8/23/06, Dikshit Saumya-G19445 <somc@motorola.com> wrote:
> > Hi
> > I am a newbie to this mailing list.
> >
> > I have a couple of queries:> >
> > 1. Can methods be part of an array definition, which can bear
> > similarity to 'C' definitions
> > of "array of function pointers", as part of a state machine
> > implementation.
>
> Look up Object#method and Method in the docs:
>
> you can do ['aaa'.method(:size), 10.method("+")]
>
> UnboundMethod could be useful as well.
>
> Other than that, its possible to call methods by name. I you just need

> to call methods on one object, it is enough to store just their names
> (preferably as symbols):
>
> methods = [:size, :length, :whatever]
>
> then you would for example do: methods.each {|m| 'aaaa'.send(m) }
>
>


Another approach would be to use Procs in the array instead of methods,
this would allow you to store arguments in the array entries as well,
and to execute more than just a method.

actions = [
lambda {obj.size},
lambda {obj.length},
lambda {obj.foo(3, "abc") }
lambda {puts "Hi Mom!"; obj.callhome } ]

then to invoke an action it's just

actions[n].call

as a variation on this, if the blocks have parameters, you can use
call(arg1, [arg2,...]) to pass parameters.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...



Dikshit Saumya-G19445

8/25/2006 4:56:00 AM

0


I have few more queries

How can we make C routine call a "method" defined in a ruby script ?
I am familiar with extending ruby to call C routines with/without swig.
But I was not able to find a easy solution for it. The only way it may
workout is spawning the script in a different process/thread from C code
and then make it do all query/answer sequence by calling a C routine. Is
there a easy way out?
Can't we make a C routine call a specific method in a Ruby Script.

Can we access a C stack from Ruby or vice versa while debugging the
Ruby extension.

Regards,
Saumya.


-----Original Message-----
From: Rick DeNatale [mailto:rick.denatale@gmail.com]
Sent: Wednesday, August 23, 2006 10:41 PM
To: ruby-talk ML
Subject: Re: Can methods be part of a array

On 8/23/06, Jan Svitok <jan.svitok@gmail.com> wrote:
> On 8/23/06, Dikshit Saumya-G19445 <somc@motorola.com> wrote:
> > Hi
> > I am a newbie to this mailing list.
> >
> > I have a couple of queries:> >
> > 1. Can methods be part of an array definition, which can bear
> > similarity to 'C' definitions
> > of "array of function pointers", as part of a state machine
> > implementation.
>
> Look up Object#method and Method in the docs:
>
> you can do ['aaa'.method(:size), 10.method("+")]
>
> UnboundMethod could be useful as well.
>
> Other than that, its possible to call methods by name. I you just need

> to call methods on one object, it is enough to store just their names
> (preferably as symbols):
>
> methods = [:size, :length, :whatever]
>
> then you would for example do: methods.each {|m| 'aaaa'.send(m) }
>
>


Another approach would be to use Procs in the array instead of methods,
this would allow you to store arguments in the array entries as well,
and to execute more than just a method.

actions = [
lambda {obj.size},
lambda {obj.length},
lambda {obj.foo(3, "abc") }
lambda {puts "Hi Mom!"; obj.callhome } ]

then to invoke an action it's just

actions[n].call

as a variation on this, if the blocks have parameters, you can use
call(arg1, [arg2,...]) to pass parameters.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...