[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

inheritance from C object with different parameter count

Geert Fannes

1/30/2006 9:12:00 AM

Hello,

I'm trying to create a derived class (ruby class) from a C-implemented
class. Everything works fine, until I tried to add a parameter to the
derived class. When I derive from a pure ruby base class, everything
works as should be, extra parameter or not. Does someone know what is
going on or what I am doing wrong?



I am getting the following output:



gfannes@lt-gfannes help $ ruby test.rb

test.rb:21:in `new': wrong number of arguments (1 for 0) (ArgumentError)

from test.rb:21





This is the ruby file containing the derivation from the base class. If
bCLib is set to false, things work as should be.

##############test.rb######################

bCLib=true



if bCLib

require('base.so')

else

class Base

def initialize()

puts("base")

end

end

end



class Derived < Base

def initialize(parameter)

puts("init derived")

super()

puts("finished super")

end

end



Derived.new('parameter')

##########################################



This is the C-file that implements the base class in C
######################base.c#####################

#include "ruby.h"

#include <stdio.h>

#include <malloc.h>



typedef struct{

double dValue;

} baseStruct;





VALUE f_new0(VALUE klass)

{

printf("f_new0.\n");

VALUE rBase;

baseStruct *pBase=malloc(sizeof(baseStruct));

rBase = Data_Wrap_Struct(klass, 0, free, pBase);

printf("before rb_obj_call_init.\n");

rb_obj_call_init(rBase, 0, 0);

printf("f_new0 finished.\n");

return rBase;

}



VALUE f_initialize0(VALUE self)

{

printf("f_initialize0.\n");

return self;

}



VALUE cBase;



void Init_base()

{

cBase=rb_define_class("Base",rb_cObject);

rb_define_singleton_method(cBase, "new", f_new0, 0);

rb_define_method (cBase, "initialize", f_initialize0, 0); }
#################################################



Everything is compiled using extconf.rb

#############extconf.rb##########################

require 'mkmf'

create_makefile('base')

#################################################



6 Answers

Esteban Manchado Velázquez

1/30/2006 8:44:00 PM

0

On Mon, Jan 30, 2006 at 06:12:13PM +0900, Geert Fannes wrote:
> Hello,
>
> I'm trying to create a derived class (ruby class) from a C-implemented
> class. Everything works fine, until I tried to add a parameter to the
> derived class. When I derive from a pure ruby base class, everything
> works as should be, extra parameter or not. Does someone know what is
> going on or what I am doing wrong?

Hmmm... weird. If you comment the "super()" call in Derived#initialize, it
dies with the exact same error. In fact, you don't even get the "init derived"
message.

After investigating a little, I found out that changing the definition of
new to:

rb_define_singleton_method(cBase, "new", f_new0, -1);

makes it work. I don't know if that's entirely correct (I'm not very familiar
with C extensions), but it works :-) I guess the problem is that, as you don't
redefine the "new" method in the class Derived, you get the Base one, so it
chokes. I don't know if the usual thing here is always defining "new" methods
with any parameter count...

HTH,

--
Esteban Manchado Velázquez <zoso@foton.es> - http://ww...
EuropeSwPatentFree - http://EuropeSwPatentFree.his...

George Ogata

1/31/2006 2:05:00 PM

0

"Geert Fannes" <Geert.Fannes@ikanconsulting.com> writes:

> I'm trying to create a derived class (ruby class) from a C-implemented
> class. Everything works fine, until I tried to add a parameter to the
> derived class. When I derive from a pure ruby base class, everything
> works as should be, extra parameter or not. Does someone know what is
> going on or what I am doing wrong?

Your C code does not do:

class Base
def initialize()
puts("base")
end
end

It's more like:

class Base
def self.new
...
end
def self.initialize
...
end
end

Thus, Derived.new(arg) is an error, since it calls Base.new, which
you've defined to take no args.

Note that Blah.new is a completely different method to
Blah#initialize. Blah.new would be a singleton method of the Class
instance Blah, but you usually don't want that, as the standard
Class#new is sufficiently magical to do everything you need. In
pseudoruby, it looks like:

class Class
def new(*args)
object = self.alloc_func
object.initialize(*args)
return object
end
end

Object#dup and Object#clone are similar:

class Object
def dup(other)
object = self.alloc_func
object.initialize_copy(other)
return object
end
def clone(other)
object = self.alloc_func
object.singleton_class = other.singleton_class.copy
# ... some other diddly bits ...
object.initialize_copy(other)
return object
end
end

So what you normally do is:

-- define the alloc func to alloc a ruby object from heap memory:
VALUE Thingy_allocate(VALUE klass) {
struct Thingy *thingy;
return Data_Make_Struct(klass, struct Thingy,
Thingy_mark, Thingy_free, thingy);
}

-- define an #initialize method:
VALUE Thingy_initialize(VALUE self, ...args...) {
...
}

-- define an #initialize_copy method:
VALUE Thingy_initialize_copy(VALUE self, VALUE other) {
...
}

-- bind 'em:
void Init_libthingies {
VALUE cThingy;
cThingy = rb_define_class("Thingy", rb_cObject);
rb_define_alloc_func(cThingy, Thingy_allocate);
rb_define_method(cThingy, "initialize", Thingy_initialize, num_args);
rb_define_method(cThingy, "initialize", Thingy_initialize_copy, 1);
...
}

Of course, #initialize_copy is optional (in fact, so is #initialize),
but the point is that doing things this way means you only have to do
the malloc-ing once (in the alloc func), and everywhere else you're
dealing with a perfectly valid ruby object. No need to call
#initialize or obj_call_init() or worry about your object not being
allocated if #initialize is overriden or gets interrupted by an
exception... just let the standard methods call your hooks and lie
back. :-)

gig

7/25/2008 6:56:00 PM

0

"Lotus_Bloom" <dwallin@tx.rr.com> wrote in message
news:77885e6e-62e3-42ed-863b-e6dd13935a34@c58g2000hsc.googlegroups.com...
On Jul 25, 1:19 pm, Deborah <debo...@modabelam.com> wrote:
> On Fri, 25 Jul 2008 04:53:46 -0700 (PDT), Lotus_Bloom
>
>
>
>
>
> <dwal...@tx.rr.com> wrote:
> >On Jul 25, 12:11 am, Deborah <debo...@modabelam.com> wrote:
> >> On Fri, 25 Jul 2008 04:04:31 GMT, Gene <g...@chewbacca.org> wrote:
> >> >Lotus_Bloom <dwal...@tx.rr.com> rote in news:27264acf-ff38-4c3b-849d-
> >> >8012b3b51...@m36g2000hse.googlegroups.com:
>
> >> >>> I was in the galactic heart of it all during the sixties. I was
> >> >>> there
> >> >for
> >> >> the
> >> >>> Great Human Be-In and the Summer of Love. What do you think?
>
> >> >> Oh my, do tell of your experience, sounds exciting. :)))
>
> >> >I took psychedelics in two series, the 1966-67 series, starting late
> >> >in
> >> >1966 and ending in the fall of 67, and the 1968 series, spring to fall
> >> >of
> >> >1968. This is as best as I can remember 40 years later.
>
> >> >The 66-67 series was the ordinary sort of experience-I realized I was
> >> >the
> >> >Son of God, and so was everyone else, I experienced so-called "ego
> >> >death",
> >> >and then I had a dream warning me off taking any more LSD. When I
> >> >ignored
> >> >it to take my already purchased dose, I had a mildly bad trip, where I
> >> >became depressed at how isolated and lonely I was. So I quit.
>
> >> >Right about then, I got into meditating Edgar Cayce style (which is
> >> >much
> >> >the same as Course style.} I went through the separation of the chown
> >> >chakra from the forehead chakra, and then the opening in succession of
> >> >the
> >> >others, travelling downward.
>
> >> >Then, in 1968, I wondered what would happen if I dropped acid and
> >> >meditated. I tried it, and the first few times it was like an immense
> >> >weight was on me, some responsibility it seemed I had undertaken to
> >> >awaken
> >> >people and uncover knowledge. I would work, and achieve a temporary
> >> >sense
> >> >of being the Son of God-and then, of course, come down.
>
> >> >The "height" I got to seemed to gradually increase, the inner
> >> >experience
> >> >got stranger, and the effect on other people more pronounced as I went
> >> >on.
> >> >I got used to hearing "I had the strangest dream about you last night"
> >> >from
> >> >friends, relatives, and total strangers. But people seemed to pick up
> >> >my
> >> >thoughts in other ways than dreams. The oddest case is where I was
> >> >doing
> >> >some Edgar Cayce chants, then stopped and did them mentally, which
> >> >morphed
> >> >into a contralto voice singing it, to an orchestral accompaniment,
> >> >with an
> >> >effect rather like Neptune from Holst's The Planets. A friend knocked
> >> >on my
> >> >door, and asked "What was that unearthly music I heard coming from
> >> >your
> >> >apartment?" It seems he heard my hallucination standing on the
> >> >sidewalk in
> >> >front of my second-story apartment!
>
> >> >But it's the kind of mind and thought I had then which is hardest to
> >> >explain. For one thing, it had a kind of fourth-dimensional quality,
> >> >with
> >> >me visualizing extensions and forces headed off in a dimension of what
> >> >you
> >> >might call vibrational height.
>
> >> >It all climaxed with a trip using DOM, or STP as we called it then.
> >> >Two
> >> >acquaintances came by and insisted I take it with them, and we went to
> >> >the
> >> >beach. I became aware of life forces moving in everything around me.
> >> >Then I
> >> >took the energy in, and shot up like a rocket. I saw a table made of
> >> >threads of light, with a scroll on it, also made of light, with
> >> >letters on
> >> >it spelling what my mind told me were people's true names, whatever
> >> >that
> >> >might be. Mine was added on the bottom of the scroll, in glowing red
> >> >letters like an LED. More firmly than ever I got that I was the Son of
> >> >God.
> >> >I seemed to be told, wordlessly, that I had done what I had promised
> >> >to do,
> >> >and thank you, but now it is over and I needed to stop taking
> >> >psychedelics.
>
> >> >The inevitable "I had the strangest dream about you" that time came
> >> >from my
> >> >brother-in-law. He saw me lying on a bed (my usual posture for
> >> >meditation,
> >> >though not this time at the beach) and rising out of the sea, with a
> >> >bright
> >> >light around me. A great crowd was watching from the shore, and one
> >> >called
> >> >out in an awed tone, "It's a miracle!"
>
> >> I really appreciate you sharing this, Gene. I mostly only had a good
> >> experience with acid, mostly spiritual in nature. It wasn't for me a
> >> party drug. I wrote a lot of songs on it, though, because it kept me
> >> up all night. You can't get to sleep.
>
> >> When it had shown me all it had to show me, it just stopped working.
> >> I didn't even get a buzz anymore. So I had no trouble giving it up.
>
> >> Deborah (BC)- Hide quoted text -
>
> >> - Show quoted text -
>
> >Do you still have the music you wrote?
>
> Sure.- Hide quoted text -
>
> - Show quoted text -

> > The reason I asked is because there was such amazing music that came
> > out of the era of LSD. I didn't realize a lot of the meaning until
> > later in life, late bloomer myself. :))

Drugs n. 1. Fool's gold.


Lotus_Bloom

7/25/2008 7:10:00 PM

0

On Jul 25, 1:55 pm, "gig" <os...@lycos.com> wrote:
> "Lotus_Bloom" <dwal...@tx.rr.com> wrote in message
>
> news:77885e6e-62e3-42ed-863b-e6dd13935a34@c58g2000hsc.googlegroups.com...
> On Jul 25, 1:19 pm, Deborah <debo...@modabelam.com> wrote:
>
>
>
>
>
> > On Fri, 25 Jul 2008 04:53:46 -0700 (PDT), Lotus_Bloom
>
> > <dwal...@tx.rr.com> wrote:
> > >On Jul 25, 12:11 am, Deborah <debo...@modabelam.com> wrote:
> > >> On Fri, 25 Jul 2008 04:04:31 GMT, Gene <g...@chewbacca.org> wrote:
> > >> >Lotus_Bloom <dwal...@tx.rr.com> rote in news:27264acf-ff38-4c3b-849d-
> > >> >8012b3b51...@m36g2000hse.googlegroups.com:
>
> > >> >>> I was in the galactic heart of it all during the sixties. I was
> > >> >>> there
> > >> >for
> > >> >> the
> > >> >>> Great Human Be-In and the Summer of Love. What do you think?
>
> > >> >> Oh my, do tell of your experience, sounds exciting. :)))
>
> > >> >I took psychedelics in two series, the 1966-67 series, starting late
> > >> >in
> > >> >1966 and ending in the fall of 67, and the 1968 series, spring to fall
> > >> >of
> > >> >1968. This is as best as I can remember 40 years later.
>
> > >> >The 66-67 series was the ordinary sort of experience-I realized I was
> > >> >the
> > >> >Son of God, and so was everyone else, I experienced so-called "ego
> > >> >death",
> > >> >and then I had a dream warning me off taking any more LSD. When I
> > >> >ignored
> > >> >it to take my already purchased dose, I had a mildly bad trip, where I
> > >> >became depressed at how isolated and lonely I was. So I quit.
>
> > >> >Right about then, I got into meditating Edgar Cayce style (which is
> > >> >much
> > >> >the same as Course style.} I went through the separation of the chown
> > >> >chakra from the forehead chakra, and then the opening in succession of
> > >> >the
> > >> >others, travelling downward.
>
> > >> >Then, in 1968, I wondered what would happen if I dropped acid and
> > >> >meditated. I tried it, and the first few times it was like an immense
> > >> >weight was on me, some responsibility it seemed I had undertaken to
> > >> >awaken
> > >> >people and uncover knowledge. I would work, and achieve a temporary
> > >> >sense
> > >> >of being the Son of God-and then, of course, come down.
>
> > >> >The "height" I got to seemed to gradually increase, the inner
> > >> >experience
> > >> >got stranger, and the effect on other people more pronounced as I went
> > >> >on.
> > >> >I got used to hearing "I had the strangest dream about you last night"
> > >> >from
> > >> >friends, relatives, and total strangers. But people seemed to pick up
> > >> >my
> > >> >thoughts in other ways than dreams. The oddest case is where I was
> > >> >doing
> > >> >some Edgar Cayce chants, then stopped and did them mentally, which
> > >> >morphed
> > >> >into a contralto voice singing it, to an orchestral accompaniment,
> > >> >with an
> > >> >effect rather like Neptune from Holst's The Planets. A friend knocked
> > >> >on my
> > >> >door, and asked "What was that unearthly music I heard coming from
> > >> >your
> > >> >apartment?" It seems he heard my hallucination standing on the
> > >> >sidewalk in
> > >> >front of my second-story apartment!
>
> > >> >But it's the kind of mind and thought I had then which is hardest to
> > >> >explain. For one thing, it had a kind of fourth-dimensional quality,
> > >> >with
> > >> >me visualizing extensions and forces headed off in a dimension of what
> > >> >you
> > >> >might call vibrational height.
>
> > >> >It all climaxed with a trip using DOM, or STP as we called it then.
> > >> >Two
> > >> >acquaintances came by and insisted I take it with them, and we went to
> > >> >the
> > >> >beach. I became aware of life forces moving in everything around me.
> > >> >Then I
> > >> >took the energy in, and shot up like a rocket. I saw a table made of
> > >> >threads of light, with a scroll on it, also made of light, with
> > >> >letters on
> > >> >it spelling what my mind told me were people's true names, whatever
> > >> >that
> > >> >might be. Mine was added on the bottom of the scroll, in glowing red
> > >> >letters like an LED. More firmly than ever I got that I was the Son of
> > >> >God.
> > >> >I seemed to be told, wordlessly, that I had done what I had promised
> > >> >to do,
> > >> >and thank you, but now it is over and I needed to stop taking
> > >> >psychedelics.
>
> > >> >The inevitable "I had the strangest dream about you" that time came
> > >> >from my
> > >> >brother-in-law. He saw me lying on a bed (my usual posture for
> > >> >meditation,
> > >> >though not this time at the beach) and rising out of the sea, with a
> > >> >bright
> > >> >light around me. A great crowd was watching from the shore, and one
> > >> >called
> > >> >out in an awed tone, "It's a miracle!"
>
> > >> I really appreciate you sharing this, Gene. I mostly only had a good
> > >> experience with acid, mostly spiritual in nature. It wasn't for me a
> > >> party drug. I wrote a lot of songs on it, though, because it kept me
> > >> up all night. You can't get to sleep.
>
> > >> When it had shown me all it had to show me, it just stopped working.
> > >> I didn't even get a buzz anymore. So I had no trouble giving it up.
>
> > >> Deborah (BC)- Hide quoted text -
>
> > >> - Show quoted text -
>
> > >Do you still have the music you wrote?
>
> > Sure.- Hide quoted text -
>
> > - Show quoted text -
> > > The reason I asked is because there was such amazing music that came
> > > out of the era of LSD.  I didn't realize a lot of the meaning until
> > > later in life, late bloomer myself.  :))
>
> Drugs  n. 1. Fool's gold.- Hide quoted text -
>
> - Show quoted text -

How so?

Carrie

7/26/2008 12:33:00 AM

0


"Gene" <gene@chewbacca.org> wrote in message
news:Xns9AE67101E3B70genewardsmithsbcglob@207.115.17.102...
> "Carrie" <starchild1124@yahoo.com> rote in news:g6csln$ld2$1@aioe.org:
>
>> What I thought of. You did LSD and got all into One with God and it's
>> a
>> miracle, and such.
>> But, did it last- do you still feel you are coming from that state
>> of
>> mind now?
>
> Your question assumes it is all about experience, which completely misses
> the
> point.

What was the point, if not for the experience?
To learn and grow from it. To see the potential of something you might get
into and feel without the chemicals.
Though, I suppose you could have done it for the entertainment.

>
>
>
> --
> Change but your mind on what you want to see,
> And all the world must change accordingly.


Gene Ward Smith

7/26/2008 1:48:00 AM

0

"Carrie" <starchild1124@yahoo.com> rote in news:g6drcc$imp$1@aioe.org:

> What was the point, if not for the experience?

Doing something. It was my strong impression that is what happened.

--
Change but your mind on what you want to see,
And all the world must change accordingly.