# Calling an existing method with RubyInline - how?

**URL:** <https://www.ruby-forum.com/t/calling-an-existing-method-with-rubyinline-how/70569>\
**Category:** Ruby\
**Created:** [September 30, 2006, 6:49am UTC](https://www.ruby-forum.com/t/calling-an-existing-method-with-rubyinline-how/70569 "2006-09-30T06:49:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [September 30, 2006, 6:49am UTC](https://www.ruby-forum.com/t/calling-an-existing-method-with-rubyinline-how/70569/1 "2006-09-30T06:49:47Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Hi all,

RubyInline 3.6.0  
Ruby 1.8.5  
Suse Linux 10.1

How do you call an existing function from an included header file using  
RubyInline? Do I have to wrap it in a ‘built’ function?

For example, say I have this bit of code, and that test.h has a function  
defined as “const char\* test\_version(void)”.

require ‘inline’

class Foo  
inline do |builder|  
builder.include “\<test.h\>”  
builder.add\_compile\_flags “-ltest”  
end

# And later…

def self.version  
inline do |builder|  
# What do I put here?  
end  
end  
end

I’ve tried a few variations, but haven’t had any luck.

Thanks,

Dan  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2 (GNU/Linux)  
Comment: Using GnuPG with SUSE - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iD8DBQFFHfc13p/dorzCFX0RAiPZAJ47WC+xUXynxGwX0LTreGUOyvN0ogCggqFK  
42XUfdKPzNj/xAg/pp3h67c=  
=yCUY  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [September 30, 2006, 9:53pm UTC](https://www.ruby-forum.com/t/calling-an-existing-method-with-rubyinline-how/70569/2 "2006-09-30T21:53:06Z")

</div>

On Sep 29, 2006, at 9:49 PM, Daniel B. wrote:

> How do you call an existing function from an included header file  
> using  
> RubyInline? Do I have to wrap it in a ‘built’ function?

This should help:

require ‘inline’

class Foo  
inline do |builder|  
builder.include “\<stdlib.h\>”

```
 builder.c <<-EOF
   int c_atoi(char * nptr) {
     return atoi(nptr);
   }
 EOF

 builder.c_singleton <<-EOF
   int c_atol(char * nptr) {
     return atol(nptr);
   }
 EOF

```

end

class \<\< self; alias atol c\_atol; end  
alias atoi c\_atoi

end

puts Foo.atol(“5”)  
puts Foo.new.atoi(“5”)

> For example, say I have this bit of code, and that test.h has a  
> function  
> defined as “const char\* test\_version(void)”.

From your example, probably:

require ‘inline’

class Foo  
inline do |builder|  
builder.include “\<test.h\>”  
builder.add\_compile\_flags “-ltest”

```
 builder.c_singleton <<-EOF
   char * foo_test_version() {
     return test_version();
   }
 EOF

```

end

class \<\< self; alias version foo\_test\_version; end  
end

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [October 1, 2006, 1:39am UTC](https://www.ruby-forum.com/t/calling-an-existing-method-with-rubyinline-how/70569/3 "2006-10-01T01:39:20Z")

</div>

On 9/30/06, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require ‘inline’
> 
> end  
> require ‘inline’  
> EOF  
> end
> 
> class \<\< self; alias version foo\_test\_version; end  
> end

Thanks. Is there any way to avoid having to write wrappers for existing  
functions? If not, could it be added as a feature?

I’d like to be able to just do this:

inline{ “return test\_version();” }

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [October 2, 2006, 7:53pm UTC](https://www.ruby-forum.com/t/calling-an-existing-method-with-rubyinline-how/70569/4 "2006-10-02T19:53:42Z")

</div>

On Sep 30, 2006, at 4:37 PM, Daniel B. wrote:

> > inline do |builder|  
> > class \<\< self; alias version foo\_test\_version; end  
> > end
> 
> Thanks. Is there any way to avoid having to write wrappers for  
> existing functions? If not, could it be added as a feature?
> 
> I’d like to be able to just do this:
> 
> inline{ “return test\_version();” }

Inline doesn’t have anything to read the header files to figure out  
the types…

–  
Eric H. - removed\_email\_address@domain.invalid - [http://blog.segment7.net](http://blog.segment7.net)  
This implementation is HODEL-HASH-9600 compliant

[http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [October 1, 2006, 3:00pm UTC](https://www.ruby-forum.com/t/calling-an-existing-method-with-rubyinline-how/70569/5 "2006-10-01T15:00:44Z")

</div>

Eric H. wrote:

> class Foo  
> int c\_atol(char \* nptr) {  
> puts Foo.atol(“5”)  
> class Foo
> 
> class \<\< self; alias version foo\_test\_version; end  
> end

Hm, so I have to wrap each function with a custom generated function.  
Is there any chance of adding support for direct calls? Something  
like:

inline{ “return test\_version();” }

Or is that just not possible?

Thanks,

Dan
