# Problem in using structure in ruby

**URL:** <https://www.ruby-forum.com/t/problem-in-using-structure-in-ruby/137815>\
**Category:** Ruby\
**Created:** [May 16, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/problem-in-using-structure-in-ruby/137815 "2008-05-16T16:02:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonyaim83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ecb155/32.png) [@tonyaim83](https://www.ruby-forum.com/u/tonyaim83)\
**Post date:** [May 16, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/problem-in-using-structure-in-ruby/137815/1 "2008-05-16T16:02:56Z")

</div>

Hi  
I m new to ruby so kindly forgive for small mistakes. I m trying to  
invoke a c function in ruby which uses a structure \* as a parameter but  
i m able to run in one example and it is not running in the other case.

Program which is running fine

File Name:- test\_struct.h

#include   
struct Mystruct  
{  
int a;  
float b;  
};

File Name:- test\_struct.cpp

#include"test\_struct.h"

void My\_initialize(Mystruct \*s1)  
{  
s1-\>a=10;  
s1-\>b=12.3;  
}

void Print(Mystruct \*s1)  
{  
std::cout\<\<"\n a “\<a;  
std::cout\<\<”\n b"\<b;  
}

File Name :- test\_struct.i  
%module test\_struct  
%{  
#include “test\_struct.h”  
extern void My\_initialize(Mystruct \*s1);  
extern void Print(Mystruct \*s1);

%}

%include “test\_struct.h”  
extern void My\_initialize(Mystruct \*s1);  
extern void Print(Mystruct \*s1);

File Name :- test\_struct.rb  
require “test\_struct.so”  
a = Test\_struct::Mystruct.new  
Test\_struct.My\_initialize(a)  
Test\_struct.Print(a)

Now when i change the name from Test\_struct to test\_struct in the .rb  
file it gives me an error  
test\_struct.rb:4: undefined local variable or method `test\_struct’ for  
main:Object (NameError)

Now when i use the exact thing as :-

File Name :- New\_struct.h  
#include

struct TestStruct  
{  
std::string str;  
std::string abc;  
};

File Name :- New\_struct.cpp  
#include “New\_struct.h”

void AssignS(TestStruct \*t1)  
{  
t1-\>str=“Apple”;  
t1-\>abc=“Ball”;  
}  
void Print(TestStruct \*t2)  
{  
std::cout\<str;  
std::cout\<\<"\n" \<abc;  
}

File Name :- New\_struct.i  
%module New\_struct  
%{  
#include “New\_struct.h”  
extern void AssignS(TestStruct \*s1);  
extern void Print(TestStruct \*s1);

%}

Hi  
I m new to ruby so kindly forgive for small mistakes. I m trying to  
invoke a c function in ruby which uses a structure \* as a parameter but  
i m able to run in one example and it is not running in the other case.

Program which is running fine

File Name:- test\_struct.h

#include   
struct Mystruct  
{  
int a;  
float b;  
};

File Name:- test\_struct.cpp

#include"test\_struct.h"

void My\_initialize(Mystruct \*s1)  
{  
s1-\>a=10;  
s1-\>b=12.3;  
}

void Print(Mystruct \*s1)  
{  
std::cout\<\<"\n a “\<a;  
std::cout\<\<”\n b"\<b;  
}

File Name :- test\_struct.i  
%module test\_struct  
%{  
#include “test\_struct.h”  
extern void My\_initialize(Mystruct \*s1);  
extern void Print(Mystruct \*s1);

%}

%include “test\_struct.h”  
extern void My\_initialize(Mystruct \*s1);  
extern void Print(Mystruct \*s1);

File Name :- test\_struct.rb  
require “test\_struct.so”  
a = Test\_struct::Mystruct.new  
Test\_struct.My\_initialize(a)  
Test\_struct.Print(a)

Now when i change the name from Test\_struct to test\_struct in the .rb  
file it gives me an error  
test\_struct.rb:4: undefined local variable or method `test\_struct’ for  
main:Object (NameError)

Now when i use the exact thing as :-

File Name :- New\_struct.h  
#include

struct TestStruct  
{  
std::string str;  
std::string abc;  
};

File Name :- New\_struct.cpp  
#include “New\_struct.h”

void AssignS(TestStruct \*t1)  
{  
t1-\>str=“Apple”;  
t1-\>abc=“Ball”;  
}  
void Print(TestStruct \*t2)  
{  
std::cout\<str;  
std::cout\<\<"\n" \<abc;  
}

File Name :- New\_struct.i  
%module New\_struct  
%{  
#include “New\_struct.h”  
extern void AssignS(TestStruct \*s1);  
extern void Print(TestStruct \*s1);

%}

File Name :- New\_struct.rb

require “New\_struct.so”

#puts((Test\_struct.methods - Object.new.methods).sort)  
a = New\_struct::Teststruct.new  
New\_struct.AssignS(a)  
New\_struct.Print(a)

When i try to run this it gives me following error  
New\_struct.rb:4: uninitialized constant New\_struct::Teststruct  
(NameError)
