# Problem in Extending ruby

**URL:** <https://www.ruby-forum.com/t/problem-in-extending-ruby/89629>\
**Category:** Ruby\
**Created:** [March 21, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/problem-in-extending-ruby/89629 "2007-03-21T17:47:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ravinder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3bc359/32.png) [@ravinder](https://www.ruby-forum.com/u/ravinder)\
**Post date:** [March 21, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/problem-in-extending-ruby/89629/1 "2007-03-21T17:47:48Z")

</div>

I want to call C code from my ruby class. I am using SWIG for doing  
this. I am able to compile the code and also able to invoke a C function  
from ruby, but am unable to access a variable.  
I have declared the variable as follows in “test.h” file:

typedef struct GenParams\_s {  
Version a\_version; /**\< Version \*/  
BOOL keep\_ignorable; /**\< Keep Ignorable Whitespaces (Default:  
FALSE) \*/  
BOOL use\_strtbl; /\*\*\< Generate String Table (Default: TRUE)  
\*/  
} GenParams;

typedef enum Version\_e {  
VERSION\_UNKNOWN = -1, /**\< Unknown Version \*/  
VERSION\_10 = 0x00, /**\< 1.0 Token \*/  
VERSION\_11 = 0x01, /**\< 1.1 Token \*/  
VERSION\_12 = 0x02, /**\< 1.2 Token \*/  
VERSION\_13 = 0x03 /\*\*\< 1.3 Token \*/  
} Version;

#define BOOL unsigned char

Now I have another class “Convert.c”, in which I have declared the  
functions which I want to call from ruby. One of the method has  
following signature:

A\_DECLARE(Error) a\_conv\_a2b\_withlen(UTINY \*a, ULONG a\_len, UTINY \*\*b,  
ULONG \*b\_len,  
GenParams \*params)

UTINY and ULONG are declared as unsigned char and unsigned long  
respectively. I am importing “test.h” file in “Convert.c” file.

Next I compile “Convert.c” file using nmake etc…(as required by SWIG).  
Now I want to access the method defined in Convert.c file, which I am  
doing as follows:

require ‘converter’

params = GenParams.new  
convert = Converter.a\_conv\_a2b\_withlen(“a sample test”, 13, “”, 100,  
params)

and getting following error:  
“uninitialized constant GenParams (NameError)”

I’d appreciate if anyone can help me in this case or suggest me a  
pointer as to where to look.

Thanks,  
Ravinder Singh

---

<div class="post-metadata">

**Author:** ![ravinder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3bc359/32.png) [@ravinder](https://www.ruby-forum.com/u/ravinder)\
**Post date:** [March 22, 2007, 8:41am UTC](https://www.ruby-forum.com/t/problem-in-extending-ruby/89629/2 "2007-03-22T08:41:45Z")

</div>

hi,  
look at [http://whytheluckystiff.net/ruby/pickaxe/](http://whytheluckystiff.net/ruby/pickaxe/)

There’ re some issues to think about when you use structs and variables  
in  
Ruby and C.

I don’ t have the time to get deeper at the moment. Perhaps later.

regards,  
Alex.

---

<div class="post-metadata">

**Author:** ![ravinder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3bc359/32.png) [@ravinder](https://www.ruby-forum.com/u/ravinder)\
**Post date:** [March 23, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/problem-in-extending-ruby/89629/3 "2007-03-23T23:11:28Z")

</div>

On Mar 21, 5:47 pm, ravinder [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \*/  
> } Version;
> 
> convert = Converter.a\_conv\_a2b\_withlen(“a sample test”, 13, “”, 100,
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

May be you have to use something like Converter::GenParams.new. I  
suggest you to look inside the generated file from swig to know how  
the structure GenParams is converted.
