[#1816] Ruby 1.5.3 under Tru64 (Alpha)? — Clemens Hintze <clemens.hintze@...>

Hi all,

17 messages 2000/03/14

[#1989] English Ruby/Gtk Tutorial? — schneik@...

18 messages 2000/03/17

[#2241] setter() for local variables — ts <decoux@...>

18 messages 2000/03/29

[ruby-talk:02227] A few questions

From: Clemens Hintze <c.hintze@...>
Date: 2000-03-28 23:51:51 UTC
List: ruby-talk #2227
Andrew Hunt writes:
> 
> Hi all,

Hi Andy,

> 
> A couple of questions have come up that I need some input on.  These
> may get kind of low-level, so hang on...
> 
> 1. ... Does an extension module have to be in this file to be built
> (it would appear not)?

No it has not to be in this file. Simple add an directory to 'ext' and
create a MANIFEST file for that extension within its directory. Then
it will be build automagically during the next make run.

I would propose, that you also describe how to build and install
extensions if they would *not* be in Ruby's ext dir. Sorry if you have 
already thought of this...

> 2. What's the difference between funcall2 and funcall3?

It has something to do, how methods are searched in the parent classes 
or what to do if they wasn't find there, AFAIK. But I do not know any
specific. So better let this be answered by others... !

> 
> 3. Is Struct still used, or is it deprecated?  If it's still used, how is 
> it different from Data (Data_Wrap_Struct, etc.)

AFAIK, Struct is a normal class in Ruby that allows you to simulate C
struct like datatypes. That means a Struct instance do not represent a 
C struct, but allow to encapsulate some member to group them together. 
You could also use a class here, but using Struct is more comfortable, 
as you have not to deal with the class creation overhead (syntactically).
For example (from the manual):

   dog = Struct.new("Dog", :name, :age)
   fred = dog.new("fred", 5)    # or fred = Struct::Dog::new("fred", 5)
   fred.age = 6
   printf "name:%s age:%d", fred.name, fred.age

without using Struct instances you would have to use:

   class Dog
      attr :name, true
      attr :age, true
      
      def initialize(*args)
         @name, @age = args
      end
   end

   fred = Dog.new("fred", 5)
   fred.age = 6
   printf "name:%s age:%d", fred.name, fred.age


The Data class, however, is more for internal use -- means: used in
extensions. Instances of it will be used to wrap a C pointer (mostly
pointing to a struct). It also contains an information to which class
it should belongs to; that means instances of Data often fake to be
instances of other self-written classes. For example (from
ext/dgm/dbm.c):

   struct dbmdata {
       int  di_size;
       DBM *di_dbm;
   }; 

   static VALUE
   fdbm_s_open(argc, argv, klass)
     int argc;
     VALUE *argv;
     VALUE klass;
   {
     struct dbmdata *dbmp;
     VALUE obj;
   
     :
     
     obj = Data_Make_Struct(klass, struct dbmdata, 0, free_dbm, dbmp);
     dbmp->di_dbm = dbm;
     dbmp->di_size = -1;

     return obj;
   }

Here an instance of class Data will be created. This instance is
wrapping a pointer to an allocated dbmdata struct. Furthermore this
Data instance will fake itself to be a DBM instance! So ruby thinks it
is dealing with a DBM instance instead of a Data instance.

All understandings removed? ... ;-))))

> 
> Thanks!
> 
> /\ndy

It is my pleasure! :-)

\cle

-- 
Clemens Hintze  mailto: c.hintze@gmx.net

In This Thread

Prev Next