Creating a C extension in Ruby

I am trying to create a C extension for ruby. I am very new to
development and have only just became familiar with ruby, so it im
really lost. I have looked at “How to create a C Extension in Ruby in
under five minutes” and it makes sense. The problem is that I dont know
what program(s) to use to compile the C code and the ruby code. I have
been using text documents for my ruby programming, so I have never
needed to compile them. So here are my questions. If I want to make a C
extension (following the model previously mentioned) what IDE do I use,
and what file format do I compile to? Also, where do I place the ruby
file and the C compiled file? And finally, will a simple notepad editor
work for this ruby development. Remember, I am new to programming in
general, so these questions may appear naive. Thanks.

On 08/14/2012 08:12 AM, Andy P. wrote:

work for this ruby development. Remember, I am new to programming in
general, so these questions may appear naive. Thanks.
What does your C extension do? It sounds a bit like you are jumping in
the deep end with jeans and a woolly jumper on =]

Sam

Well eventually it will communicate with a piece of hardware programmed
in C and retrieve data. For now, it should return 10. Here is the link
to the tutorial that I am basing this off of.

http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html

The final product will be a SketchUp Plugin.

I suggest you read this for a quick primer:
http://blog.jcoglan.com/2012/07/29/your-first-ruby-native-extension-c/

– Matma R.

Andy P. wrote in post #1072253:

I am trying to create a C extension for ruby. I am very new to
development and have only just became familiar with ruby, so it im
really lost. I have looked at “How to create a C Extension in Ruby in
under five minutes” and it makes sense. The problem is that I dont know
what program(s) to use to compile the C code and the ruby code.

There’s a gcc command line example in this doc:

http://www.ruby-doc.org/docs/ProgrammingRuby/html/ext_ruby.html

Make sure you have the ruby development libraries (e.g. in Ubuntu you
need ruby-dev and build-essential). That’s another thing - this all
varies very much depending on which platform you’re trying to use, and
you haven’t even given this most basic piece of information.

If you are new to C as well, this is probably not the best place to
start.

You could try the RubyInline gem for embedding small snippets of C
directly in your Ruby, which might be useful depending on what it is
you’re actually trying to achieve.

Thanks for all the help. I have actually made the C extension but now I
have to integrate it with sketchup.

On Aug 13, 2012, at 4:38 PM, Andy P. wrote:

Well eventually it will communicate with a piece of hardware programmed
in C and retrieve data. For now, it should return 10. Here is the link
to the tutorial that I am basing this off of.

http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html

The final product will be a SketchUp Plugin.

Perhaps you haven’t seen this:
http://www.sketchup.com/intl/en/download/rubyscripts.html

Or, perhaps you have since you mention H/W comms. Would it be easier for
the communication to happen from Ruby? (Since you don’t define the means
of the communication.)

-Rob

So the extension is running. If I type require ‘C:~~~~~\mytest’ in IRB
then include MyTest and then type puts test1 it returns 10 (like it is
supposed to). The end goal is to have this working in SketchUp, and it
seems to glitch when I try to call the .so file outside of IRB.

This is the call in the IRB interface (the names have changed but they
are still representative of the same thing)

irb(main):001:0> require ‘C:\Program Files (x86)\Google\Google SketchUp
8\Plugins\manet’
=> true
irb(main):002:0> include Thoreau
=> Object
irb(main):003:0> puts waldenpond
10
=> nil
irb(main):004:0>

Now, if I write a short ruby file and place it in sketchup, like

require ‘manet’

It crashes. I think it might have something to do with the .so versus
the .dll file? any help would be nice.

What version of Ruby are you using? What version is SketchUp using? If
these are different, it won’t work.

– Matma R.

Sorry for the sporadic posting. I have figured out the plugin. The
extension works in SketchUp now. I had to download a older version of
ruby and change some lines of the config.h file. Then i had to compile
the makefile through nmake, not make in the VS 2010 command prompt.
Thanks.