User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Ruby section within the Web Development category of DaniWeb, a massive community of 425,772 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,314 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Ruby advertiser: SELL YOUR PRODUCT TODAY !
Views: 1255 | Replies: 0
Join Date: Aug 2007
Posts: 48
Reputation: tonyaim83 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tonyaim83 tonyaim83 is offline Offline
Light Poster

Problem in using structure in ruby

  #1  
May 16th, 2008
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
  1. #include <iostream>
  2. struct Mystruct
  3. {
  4. int a;
  5. float b;
  6. };

File Name:- test_struct.cpp
  1. #include"test_struct.h"
  2.  
  3. void My_initialize(Mystruct *s1)
  4. {
  5. s1->a=10;
  6. s1->b=12.3;
  7. }
  8.  
  9. void Print(Mystruct *s1)
  10. {
  11. std::cout<<"\n a "<<s1->a;
  12. std::cout<<"\n b"<<s1->b;
  13. }
File Name :- test_struct.i
  1. %module test_struct
  2. %{
  3. #include "test_struct.h"
  4. extern void My_initialize(Mystruct *s1);
  5. extern void Print(Mystruct *s1);
  6.  
  7. %}
  8.  
  9. %include "test_struct.h"
  10. extern void My_initialize(Mystruct *s1);
  11. extern void Print(Mystruct *s1);
File Name :- test_struct.rb
  1. require "test_struct.so"
  2. a = Test_struct::Mystruct.new
  3. Test_struct.My_initialize(a)
  4. 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
  1. #include<iostream>
  2.  
  3. struct TestStruct
  4. {
  5. std::string str;
  6. std::string abc;
  7. };
File Name :- New_struct.cpp

  1. #include "New_struct.h"
  2.  
  3. void AssignS(TestStruct *t1)
  4. {
  5. t1->str="Apple";
  6. t1->abc="Ball";
  7. }
  8. void Print(TestStruct *t2)
  9. {
  10. std::cout<<t2->str;
  11. std::cout<<"\n" <<t2->abc;
  12. }
File Name :- New_struct.i
  1. %module New_struct
  2. %{
  3. #include "New_struct.h"
  4. extern void AssignS(TestStruct *s1);
  5. extern void Print(TestStruct *s1);
  6.  
  7. %}

File Name :- New_struct.rb
  1. require "New_struct.so"
  2.  
  3. #puts((Test_struct.methods - Object.new.methods).sort)
  4. a = New_struct::Teststruct.new
  5. New_struct.AssignS(a)
  6. 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)
Last edited by peter_budo : May 16th, 2008 at 5:37 pm. Reason: Swaping forward slash to back slash in first code tag
AddThis Social Bookmark Button
Reply With Quote  

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Ruby Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Ruby Forum

All times are GMT -4. The time now is 2:45 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC