bash# g++ -c rubytest.cpp –g –Wall -I/usr/lib/ruby/1.8/include \ -I/usr/local/include/rice/include bash# g++ -shared –o rubytest.so rubytest.o -L/usr/lib/ruby/1.8/lib \ -L/usr/local/lib/rice/lib -lruby –lrice –ldl -lpthread bash# cp rubytest.so /opt/test bash# export RUBYLIB=$RUBYLIB:/opt/test bash# irb irb> require 'rubytest' => true
#include "rice/Class.hpp"
extern "C"
void Init_rubytest( ) {
Class tmp_ = define_class("Test");
}
irb> require ‘rubytest' => true irb> a = Test.new => #<Test:0x1084a3928> irb> a.methods => ["inspect", "tap", "clone", "public_methods", "__send__", "instance_variable_defined?", "equal?", "freeze", …]
void hello() {
std::cout << "Hello World!";
}
extern "C"
void Init_rubytest() {
Class test_ = define_class("Test")
.define_method("hello", &hello);
}
irb> require ‘rubytest' => true irb> Test.new.hello Hello, World! => nil
void hello(std::string args) {
std::cout << args << std::endl;
}
extern "C"
void Init_rubytest() {
Class test_ = define_class("Test")
.define_method("hello", &hello);
}
irb> a = Test.new <Test:0x0145e42112> irb> a.hello "Hello World in Ruby" Hello World in Ruby => nil
#include "rice/Array.hpp"
void Array_Print (Array a) {
Array::iterator aI = a.begin();
Array::iterator aE = a.end();
while (aI != aE) {
std::cout << "Array has " << *aI << std::endl;
++aI;
}
}
>> t = Test.new => #<Test:0x100494688> >> t.Array_Print ["g", "ggh1", "hh1"] ArgumentError: Unable to convert Array to std::vector<std::string, std::allocator<std::string> > from (irb):3:in `hello' from (irb):3
>> t = Test.new => #<Test:0x100494688> >> t.Array_Print ["hello", "world", "ruby"] Array has hello Array has world Array has ruby => nil
#include "rice/String.hpp"
#include "rice/Array.hpp"
using namespace rice;
Array return_array (Array a) {
Array tmp_;
tmp_.push(1);
tmp_.push(2.3);
tmp_.push(String("hello"));
return tmp_;
}
>> x = t.return_array => [1, 2.3, "hello"] >> x[0].class => Fixnum >> x[1].class => Float >> x[2].class => String
void print_array(std::vector<std::string> args)
template <typename T> T from_ruby(Object );
template<>
std::vector<std::string> from_ruby< std::vector<std::string> > (Object o) {
Array a(o);
std::vector<std::string> v;
for(Array::iterator aI = a.begin(); aI != a.end(); ++aI)
v.push_back(((String)*aI).str());
return v;
}
template<>
std::vector<std::string> from_ruby< std::vector<std::string> > (Object o) {
Array a(o);
std::vector<std::string> v;
for(Array::iterator aI = a.begin(); aI != a.end(); ++aI)
v.push_back(from_ruby<std::string> (*aI));
return v;
}
template<>
inline std::string from_ruby<std::string>(Rice::Object x) {
return Rice::String(x).str();
}
>> t = Test.new => #<Test:0x10e71c5c8> >> t.print_array ["aa", "bb"] aa bb => nil >> t.print_array ["aa", "bb", 111] TypeError: wrong argument type Fixnum (expected String) from (irb):4:in `print_array' from (irb):4
template<>
std::vector<std::string> from_ruby< std::vector<std::string> > (Object o) {
Array a(o);
std::vector<std::string> v;
for(Array::iterator aI = a.begin(); aI != a.end(); ++aI)
v.push_back(aI->to_s().str());
return v;
}
void init(Object self) {
self.iv_set("@intvar", 121);
self.iv_set("@stringvar", String("testing"));
}
Class cTest = define_class("Test").
define_method("initialize", &init);
>> require 'rubytest' => true >> t = Test.new => #<Test:0x1010fe400 @stringvar="testing", @intvar=121>
void init(Object self) {
self.iv_set("@intvar", 121);
self.iv_set("@stringvar", String("testing"));
}
int getvalue(Object self) {
return self.iv_get("@intvar");
}
Class cTest = define_class("Test").
define_method("initialize", &init).
define_method("getint", &getvalue);
class cppType {
public:
void print(String args) {
std::cout << args.str() << endl;
}
};
Class rb_cTest =
define_class<cppType>("Test")
.define_method("print", &cppType::print);
>> t = Test.new TypeError: allocator undefined for Test from (irb):3:in `new' from (irb):3
#include "rice/Constructor.hpp"
#include "rice/String.hpp"
class cppType {
public:
void print(String args) {
std::cout << args.str() << endl;
}
};
Class rb_cTest =
define_class<cppType>("Test")
.define_constructor(Constructor<cppType>())
.define_method("print", &cppType::print);
class cppType {
public:
cppType(int m) {
std::cout << m << std::endl;
}
cppType(Array a) {
std::cout << a.size() << std::endl;
}
void print(String args) {
std::cout << args.str() << endl;
}
};
Class rb_cTest =
define_class<cppType>("Test")
.define_constructor(Constructor<cppType, int>())
.define_constructor(Constructor<cppType, Array>())
.define_method("print", &cppType::print);
>> t = Test.new 2 TypeError: wrong argument type Fixnum (expected Array) from (irb):2:in `initialize' from (irb):2:in `new' from (irb):2 >> t = Test.new [1, 2] 2 => #<Test:0x10d52cf48>
#include "rice/Constructor.hpp"
#include "rice/String.hpp"
class cppType {
public:
void print(String args) {
std::cout << args.str() << endl;
}
};
Module rb_cModule = define_module("Types");
Class rb_cTest =
define_class_under<cppType>(rb_cModule, "Test")
.define_constructor(Constructor<cppType>())
.define_method("print", &cppType::print);
>> include Types => Object >> y = Types::Test.new [1, 1, 1] 3 => #<Types::Test:0x1058efbd8>
>> NewClass = Struct.new(:a, :ab, :aab) => NewClass >> NewClass.class => Class >> a = NewClass.new => #<struct NewClass a=nil, ab=nil, aab=nil> >> a.a = 1 => 1 >> a.ab = "test" => "test" >> a.aab = 2.33 => 2.33 >> a => #<struct NewClass a=1, ab="test", aab=2.33> >> a.a.class => Fixnum >> a.ab.class => String >> a.aab.class => Float
#include "rice/Struct.hpp"
…
Module rb1 = define_module("Types");
define_struct().
define_member("a").
define_member("ab").
define_member("aab").
initialize(rb1, "NewClass");
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有