[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Embedded Ruby Question

Brian Campbell

8/1/2006 11:54:00 PM

Hi all,

I want to embed Ruby in my C++ application. I'd like to read one or more
Ruby scripts stored in my RDBMS, pass the Ruby script as a string (not a
file name) to the interpreter, and have it execute the code. Is this
possible? If so, is there a URL that describes how to do this or shows an
example?

Thanks!

-Brian

--------------------
Brian E Campbell
becampbel@hotmail.com



16 Answers

Timothy Goddard

8/2/2006 12:48:00 AM

0

There's a good section on embedding Ruby in the Pickaxe 2. You have to
be extremely careful with some aspects of it, especially if you use
threads (the Ruby interpreter is not currently thread safe).

Brian Campbell wrote:
> Hi all,
>
> I want to embed Ruby in my C++ application. I'd like to read one or more
> Ruby scripts stored in my RDBMS, pass the Ruby script as a string (not a
> file name) to the interpreter, and have it execute the code. Is this
> possible? If so, is there a URL that describes how to do this or shows an
> example?
>
> Thanks!
>
> -Brian
>
> --------------------
> Brian E Campbell
> becampbel@hotmail.com

Brian Campbell

8/2/2006 1:28:00 AM

0

Hi Timothy,

Thanks for the reply!

Yes, I have the Pickaxe 2. Unfortunately, it doesn't describe/document,
what I need to do. It describes how to embed the interpreter, call/execute
Ruby from an existing file, extend Ruby via C/C++, but it does not describe
how to pass a string that is Ruby code from C++ to the interpreter and have
it execute. That's what I need.

Thanks.

-Brian

--------------------
Brian E Campbell
becampbel@hotmail.com




>From: "Timothy Goddard" <interfecus@gmail.com>
>Reply-To: ruby-talk@ruby-lang.org
>To: ruby-talk@ruby-lang.org (ruby-talk ML)
>Subject: Re: Embedded Ruby Question
>Date: Wed, 2 Aug 2006 09:50:10 +0900
>
>There's a good section on embedding Ruby in the Pickaxe 2. You have to
>be extremely careful with some aspects of it, especially if you use
>threads (the Ruby interpreter is not currently thread safe).
>
>Brian Campbell wrote:
> > Hi all,
> >
> > I want to embed Ruby in my C++ application. I'd like to read one or
>more
> > Ruby scripts stored in my RDBMS, pass the Ruby script as a string (not a
> > file name) to the interpreter, and have it execute the code. Is this
> > possible? If so, is there a URL that describes how to do this or shows
>an
> > example?
> >
> > Thanks!
> >
> > -Brian
> >
> > --------------------
> > Brian E Campbell
> > becampbel@hotmail.com
>
>



myjpa1@gmail.com

8/2/2006 3:20:00 AM

0

As a matter of fact, ruby's interpreter accepts the switch '-e' to
execute any string following it as ruby source code.

for instance, when you entered as following,
ruby -e "puts 5"
the console would output 5.

you may execute such shell command in C/C++.
may it be of help.

Brian Campbell wrote:
> Hi Timothy,
>
> Thanks for the reply!
>
> Yes, I have the Pickaxe 2. Unfortunately, it doesn't describe/document,
> what I need to do. It describes how to embed the interpreter, call/execute
> Ruby from an existing file, extend Ruby via C/C++, but it does not describe
> how to pass a string that is Ruby code from C++ to the interpreter and have
> it execute. That's what I need.
>
> Thanks.
>
> -Brian
>
> --------------------
> Brian E Campbell
> becampbel@hotmail.com
>
>
>
>
> >From: "Timothy Goddard" <interfecus@gmail.com>
> >Reply-To: ruby-talk@ruby-lang.org
> >To: ruby-talk@ruby-lang.org (ruby-talk ML)
> >Subject: Re: Embedded Ruby Question
> >Date: Wed, 2 Aug 2006 09:50:10 +0900
> >
> >There's a good section on embedding Ruby in the Pickaxe 2. You have to
> >be extremely careful with some aspects of it, especially if you use
> >threads (the Ruby interpreter is not currently thread safe).
> >
> >Brian Campbell wrote:
> > > Hi all,
> > >
> > > I want to embed Ruby in my C++ application. I'd like to read one or
> >more
> > > Ruby scripts stored in my RDBMS, pass the Ruby script as a string (not a
> > > file name) to the interpreter, and have it execute the code. Is this
> > > possible? If so, is there a URL that describes how to do this or shows
> >an
> > > example?
> > >
> > > Thanks!
> > >
> > > -Brian
> > >
> > > --------------------
> > > Brian E Campbell
> > > becampbel@hotmail.com
> >
> >

myjpa1@gmail.com

8/2/2006 3:21:00 AM

0

As a matter of fact, ruby's interpreter accepts the switch '-e' to
execute any string following it as ruby source code.

for instance, when you entered as following,
ruby -e "puts 5"
the console would output 5.

you may execute such shell command in C/C++.
may it be of help.

Brian Campbell wrote:
> Hi Timothy,
>
> Thanks for the reply!
>
> Yes, I have the Pickaxe 2. Unfortunately, it doesn't describe/document,
> what I need to do. It describes how to embed the interpreter, call/execute
> Ruby from an existing file, extend Ruby via C/C++, but it does not describe
> how to pass a string that is Ruby code from C++ to the interpreter and have
> it execute. That's what I need.
>
> Thanks.
>
> -Brian
>
> --------------------
> Brian E Campbell
> becampbel@hotmail.com
>
>
>
>
> >From: "Timothy Goddard" <interfecus@gmail.com>
> >Reply-To: ruby-talk@ruby-lang.org
> >To: ruby-talk@ruby-lang.org (ruby-talk ML)
> >Subject: Re: Embedded Ruby Question
> >Date: Wed, 2 Aug 2006 09:50:10 +0900
> >
> >There's a good section on embedding Ruby in the Pickaxe 2. You have to
> >be extremely careful with some aspects of it, especially if you use
> >threads (the Ruby interpreter is not currently thread safe).
> >
> >Brian Campbell wrote:
> > > Hi all,
> > >
> > > I want to embed Ruby in my C++ application. I'd like to read one or
> >more
> > > Ruby scripts stored in my RDBMS, pass the Ruby script as a string (not a
> > > file name) to the interpreter, and have it execute the code. Is this
> > > possible? If so, is there a URL that describes how to do this or shows
> >an
> > > example?
> > >
> > > Thanks!
> > >
> > > -Brian
> > >
> > > --------------------
> > > Brian E Campbell
> > > becampbel@hotmail.com
> >
> >

Logan Capaldo

8/2/2006 3:56:00 AM

0


On Aug 1, 2006, at 9:28 PM, Brian Campbell wrote:

> Hi Timothy,
>
> Thanks for the reply!
>
> Yes, I have the Pickaxe 2. Unfortunately, it doesn't describe/
> document, what I need to do. It describes how to embed the
> interpreter, call/execute Ruby from an existing file, extend Ruby
> via C/C++, but it does not describe how to pass a string that is
> Ruby code from C++ to the interpreter and have it execute. That's
> what I need.
>
> Thanks.
>
One of the best sources of documentation for C extensions is
README.EXT in the source distribution. Here is the relevant section
for what you want to do:

2.2.1 Evaluate Ruby Programs in a String

The easiest way to use Ruby's functionality from a C program is to
evaluate the string as Ruby program. This function will do the job.

VALUE rb_eval_string(const char *str)

Evaluation is done under the current context, thus current local
variables
of the innermost method (which is defined by Ruby) can be accessed.


> -Brian
>
> --------------------
> Brian E Campbell
> becampbel@hotmail.com
>
>
>
>
>> From: "Timothy Goddard" <interfecus@gmail.com>
>> Reply-To: ruby-talk@ruby-lang.org
>> To: ruby-talk@ruby-lang.org (ruby-talk ML)
>> Subject: Re: Embedded Ruby Question
>> Date: Wed, 2 Aug 2006 09:50:10 +0900
>>
>> There's a good section on embedding Ruby in the Pickaxe 2. You
>> have to
>> be extremely careful with some aspects of it, especially if you use
>> threads (the Ruby interpreter is not currently thread safe).
>>
>> Brian Campbell wrote:
>> > Hi all,
>> >
>> > I want to embed Ruby in my C++ application. I'd like to read
>> one or more
>> > Ruby scripts stored in my RDBMS, pass the Ruby script as a
>> string (not a
>> > file name) to the interpreter, and have it execute the code. Is
>> this
>> > possible? If so, is there a URL that describes how to do this
>> or shows an
>> > example?
>> >
>> > Thanks!
>> >
>> > -Brian
>> >
>> > --------------------
>> > Brian E Campbell
>> > becampbel@hotmail.com
>>
>>
>
>
>


Brian Campbell

8/2/2006 4:01:00 AM

0

Logan,

Excellent! This is exactly what I'm after. Thanks!

-Brian



--------------------
Brian E Campbell
becampbel@hotmail.com





>From: Logan Capaldo <logancapaldo@gmail.com>
>Reply-To: ruby-talk@ruby-lang.org
>To: ruby-talk@ruby-lang.org (ruby-talk ML)
>Subject: Re: Embedded Ruby Question
>Date: Wed, 2 Aug 2006 12:55:46 +0900
>
>
>On Aug 1, 2006, at 9:28 PM, Brian Campbell wrote:
>
>>Hi Timothy,
>>
>>Thanks for the reply!
>>
>>Yes, I have the Pickaxe 2. Unfortunately, it doesn't describe/ document,
>>what I need to do. It describes how to embed the interpreter,
>>call/execute Ruby from an existing file, extend Ruby via C/C++, but it
>>does not describe how to pass a string that is Ruby code from C++ to the
>>interpreter and have it execute. That's what I need.
>>
>>Thanks.
>>
>One of the best sources of documentation for C extensions is README.EXT in
>the source distribution. Here is the relevant section for what you want to
>do:
>
>2.2.1 Evaluate Ruby Programs in a String
>
>The easiest way to use Ruby's functionality from a C program is to
>evaluate the string as Ruby program. This function will do the job.
>
> VALUE rb_eval_string(const char *str)
>
>Evaluation is done under the current context, thus current local variables
>of the innermost method (which is defined by Ruby) can be accessed.
>
>
>>-Brian
>>
>>--------------------
>>Brian E Campbell
>>becampbel@hotmail.com
>>
>>
>>
>>
>>>From: "Timothy Goddard" <interfecus@gmail.com>
>>>Reply-To: ruby-talk@ruby-lang.org
>>>To: ruby-talk@ruby-lang.org (ruby-talk ML)
>>>Subject: Re: Embedded Ruby Question
>>>Date: Wed, 2 Aug 2006 09:50:10 +0900
>>>
>>>There's a good section on embedding Ruby in the Pickaxe 2. You have to
>>>be extremely careful with some aspects of it, especially if you use
>>>threads (the Ruby interpreter is not currently thread safe).
>>>
>>>Brian Campbell wrote:
>>> > Hi all,
>>> >
>>> > I want to embed Ruby in my C++ application. I'd like to read one or
>>>more
>>> > Ruby scripts stored in my RDBMS, pass the Ruby script as a string
>>>(not a
>>> > file name) to the interpreter, and have it execute the code. Is this
>>> > possible? If so, is there a URL that describes how to do this or
>>>shows an
>>> > example?
>>> >
>>> > Thanks!
>>> >
>>> > -Brian
>>> >
>>> > --------------------
>>> > Brian E Campbell
>>> > becampbel@hotmail.com
>>>
>>>
>>
>>
>>
>
>



Nobuyoshi Nakada

8/2/2006 7:17:00 AM

0

Hi,

At Wed, 2 Aug 2006 12:55:46 +0900,
Logan Capaldo wrote in [ruby-talk:205673]:
> VALUE rb_eval_string(const char *str)

It's not exception safe. Use rb_eval_string_protect() for
embedded use.

--
Nobu Nakada

Timothy Byrd

8/3/2006 12:33:00 AM

0


Brian Campbell wrote:

> I want to embed Ruby in my C++ application.

Here is some sample code I'd come up with (for 1.8.2, I think). You
may need to tweak it a bit.

-- Timothy

// r_in_c.cpp : Sample program for embedding Ruby in C.
//

//#include "stdafx.h"
#include <tchar.h>
#include "stdio.h"
#include <string>

#pragma warning(disable: 4312)

#include "ruby.h"
//#include "st.h"

void print_ruby_value(int v, int indent, const char* str)
{
VALUE val = (VALUE)(v);
int type = TYPE(v);
#if 1
switch (type) {
case T_FIXNUM:
printf("%*s`%s` ==> fixnum, value = %d\n", indent, "", str,
FIX2INT(v));
break;
case T_NIL:
printf("%*s`%s` ==> nil\n", indent, "", str);
break;
case T_FALSE:
printf("%*s`%s` ==> false\n", indent, "", str);
break;
case T_TRUE:
printf("%*s`%s` ==> true\n", indent, "", str);
break;
case T_UNDEF:
printf("%*s`%s` ==> undef\n", indent, "", str);
break;
case T_SYMBOL:
printf("%*s`%s` ==> symbol (%d)\n", indent, "", str,
SYM2ID(v));
break;
case T_STRING:
printf("%*s`%s` ==> string `%*s`\n", indent, "", str,
RSTRING(v)->len, RSTRING(v)->ptr);
break;
case T_FLOAT:
printf("%*s`%s` ==> float `%f`\n", indent, "", str,
RFLOAT(v)->value);
break;
case T_BIGNUM:
{
VALUE s = rb_String(val);
printf("%*s`%s` ==> bignum `%*s`\n", indent, "", str,
RSTRING(s)->len, RSTRING(s)->ptr);
}
break;
case T_REGEXP:
printf("%*s`%s` ==> regexp `%*s`\n", indent, "", str,
RREGEXP(v)->len, RREGEXP(v)->str);
break;
case T_ARRAY:
{
char buffer[20];
long len = RARRAY(v)->len;
VALUE* ptr = RARRAY(v)->ptr;
printf("%*s`%s` ==> array (%d):\n", indent, "", str, len);
for (long i = 0; i < RARRAY(v)->len; ++i)
{
sprintf(buffer, "a[%d]", i);
print_ruby_value(ptr[i], indent+4, "default value");
}
}
break;
case T_HASH:
{
RHash* h = RHASH(v);
printf("%*s`%s` ==> hash (iter_lev %d):\n", indent, "", str,
h->iter_lev);
print_ruby_value(h->ifnone, indent+4, "not found value");

struct st_table *tbl = h->tbl;;
printf("%*sbins = %d, entries = %d\n", indent+4, "",
tbl->num_bins, tbl->num_entries);
}
break;
default:
printf("%*s`%s` ==> class %d\n", indent, "", str, type);
break;
}
#else
switch (type) {
case T_NIL:
printf("%*s`%s` ==> nil\n", indent, "", str);
break;
case T_ARRAY:
val = rb_ary_join(val, rb_str_new2(", "));
printf("%*s`%s` ==> type(%d) `[%*s]`\n", indent, "", str, type,
RSTRING(val)->len, RSTRING(val)->ptr);
break;
case T_REGEXP:
val = rb_String(val);
printf("%*s`%s` ==> type(%d) `/%*s/`\n", indent, "", str, type,
RSTRING(val)->len, RSTRING(val)->ptr);
break;
default:
val = rb_String(val);
printf("%*s`%s` ==> type(%d) `%*s`\n", indent, "", str, type,
RSTRING(val)->len, RSTRING(val)->ptr);
}
#endif

if (indent == 0) {
printf("\n");
}
}


// Returns output of command as a string
//
// see rb_f_backquote() in io.c
//
// needs to set exit code of command into rb_last_status
//
static VALUE m_backquote(VALUE self, VALUE commandVal)
{
VALUE arr = Qnil;
char* commandStr = StringValueCStr(commandVal);

if (commandStr) {

printf("Executing backquote command on `%s`\n", commandStr);

// This should be the captured output of the command.
// I'll just reverse the string as the command value, here.
//
size_t len = strlen(commandStr);
std::string ss(len, ' ');
for (size_t i = 0, j = len - 1; i < len; ++i, --j) {
ss[i] = commandStr[j];
}

arr = rb_str_new2(ss.c_str());
}
else {
printf("Error: cannot execute object\n");
}

return arr;
}


// Executes _cmd_ in a subshell, returning true (Qtrue) if the command
// was found and ran successfully, false (Qfalse) otherwise.
//
// passed in elements may be arrays - may want to flatten and then
// join with " "s between.
//
// See rb_f_system() in process.c
//
// needs to set exit code of command into rb_last_status
//
static VALUE m_system(int argc, VALUE *argv)
{
printf("Called Kernel#system on:\n");

for (int i = 0; i < argc; ++i) {
VALUE s = rb_String(argv[i]);
printf(" %2d. `%*s`\n", i, RSTRING(s)->len, RSTRING(s)->ptr);
}

return Qtrue;
}

// Might want to have separate functions for stdout and stderr, so they
// can be separated...
//
static VALUE m_write_stdout(VALUE self, VALUE input)
{
if (TYPE(input) == T_STRING) {
printf("We have captured (stdout): \"%*s\"\n",
RSTRING(input)->len, RSTRING(input)->ptr);
// Return the number of bytes written
return
rb_fix_new(RSTRING(input)->len*sizeof(RSTRING(input)->ptr[0]));
}
else {
printf("Error: cannot write object to stdout\n");
return Qnil;
}
}
static VALUE m_write_stderr(VALUE self, VALUE input)
{
// Need to use rb_string_value if it can contain embedded nuls.
char * got = StringValueCStr(input);
printf("We have captured (stderr): \"%s\"\n", got);
// Return the number of bytes written
return rb_fix_new(strlen(got)*sizeof(got[0]));
}


int _tmain(int argc, _TCHAR* argv[])
{
NtInitialize(&argc, &argv);

ruby_init();
ruby_script("embedded");

rb_define_global_function("`", RUBY_METHOD_FUNC(m_backquote), 1);
rb_define_global_function("system", RUBY_METHOD_FUNC(m_system),
-1);

rb_define_singleton_method(rb_stdout, "write",
RUBY_METHOD_FUNC(m_write_stdout), 1);
rb_define_singleton_method(rb_stderr, "write",
RUBY_METHOD_FUNC(m_write_stderr), 1);

char* eval_strings[] = {
"3",
"nil",
"true",
"false",
"5+6",
"?c",
":fred",
"'hello'",
"\"hello\"",
"'hello there'",
"'123'*3",
"3.8",
"x = 123_456",
"x * x",
"\"#{x * x}\"",
"111_111_111**2",
"-111_111_111**3",
"[1, 'two', 3.0]",
"[[1,2,3],[4,5,6],[7,8,9]]",
"/r[iou]se/",
"`dir c:\\\\windows`",
"h = { 'dog' => 'canine', 'cat' => 'feline', 'donkey' =>
'asinine' } ",
"h.length",
"h['dog']",
"h['cow'] = 'bovine' ",
"h[12] = 'dodecine' ",
"h['cat'] = 99 ",
"h",
"puts 'this is a test'",
"$stderr.puts 'writing to stderr'",
"system('TITLE', '4NT/Ruby')",
"system('DELAY 3 & TITLE Changed window title from Ruby')",
"5",
};

const int eval_strings_len = sizeof(eval_strings) /
sizeof(eval_strings[0]);

int status = 0;

for (int i = 0; i < eval_strings_len; ++i) {
status = rb_eval_string(eval_strings[i]);
print_ruby_value(status, 0, eval_strings[i]);
}

ruby_cleanup(0);

return 0;
}

?cg?

9/1/2010 1:29:00 AM

0

On Tue, 31 Aug 2010 17:49:04 -0700 (PDT), chatnoir
<wolfbat359a@mindspring.com> wrote:

>On Aug 31, 5:48 pm, °cg° <cgrams7@{removethis}yahoo.com> wrote:
>> On Tue, 31 Aug 2010 12:00:02 -0700 (PDT), chatnoir
>>
>> <wolfbat3...@mindspring.com> wrote:
>> >Now we know why they act.   They are the heart and soul of Corporate
>> >elites now we know why they don't attack corporate greed and bad deeds
>>
>> Using your logic (not mine) we would know why the democrats do not
>> attack immigrants.  The name is George Soros.  Oh, by the way, he made
>> his money as a currency speculator, stock investor and businessman.  I
>> guess nobody told him which side he was supposed to be on.
>>
>> --
>> °cg°
>>
>> Fortune Cookie: A conclusion is simply where you
>> arrive when you get tired of thinking.
>
>Name a supposedly independent group that Soros is behind? Do they
>leave out money changing when they mention the illls of the country?

But I give you an "A" for the attempt. The phrase "supposedly
independent group" is a real gem when it comes to asking loaded
questions. But it is moot in any case.

It is your logic and not mine. My logic says that the problem is the
system and not liberals vs conservatives. It has nothing to do with
supposedly independent groups and everything to do with monied
interest groups.

As long as folks view the issues as "left vs. right", "dems vs. reps",
etc. the politicians have what the want - a stable system which is
littered with corruption and undue influence from all sorts of monied
groups of which those you name are only some.

A system that fills politician's pockets and I'll guarantee you that
they put the money in the most convenient pocket with no regard to the
pocket being a left or right pocket.. It is only chump change to the
unions, corporations of all sorts, AMA, ABA, AARP and on and on.

It isn't even chump change to the drug lords - more like pennies they
wouldn't bother to bend over to pick up if they saw it laying on the
sidewalk.

So, your view of influence peddling is going exactly down the road
that the real interest groups want you to travel. Question the
politicians and the so called political groups and you will never see
those who are pulling the strings (filling the pockets). Those who
have an interest in not congressional term limits, reduced campaign
seasons, increased pay for members of Congress, etc.

Question the system - not the elected players or the supposedly
independent groups. Then fix the system. We will all be the better
for it.

--
°cg°

Fortune Cookie: A conclusion is simply where you
arrive when you get tired of thinking.

chatnoir

9/7/2010 11:18:00 AM

0

On Aug 31, 7:29 pm, °cg° <cgrams7@{removethis}yahoo.com> wrote:
> On Tue, 31 Aug 2010 17:49:04 -0700 (PDT), chatnoir
>
>
>
>
>
> <wolfbat3...@mindspring.com> wrote:
> >On Aug 31, 5:48 pm, °cg° <cgrams7@{removethis}yahoo.com> wrote:
> >> On Tue, 31 Aug 2010 12:00:02 -0700 (PDT), chatnoir
>
> >> <wolfbat3...@mindspring.com> wrote:
> >> >Now we know why they act.   They are the heart and soul of Corporate
> >> >elites now we know why they don't attack corporate greed and bad deeds
>
> >> Using your logic (not mine) we would know why the democrats do not
> >> attack immigrants.  The name is George Soros.  Oh, by the way, he made
> >> his money as a currency speculator, stock investor and businessman.  I
> >> guess nobody told him which side he was supposed to be on.
>
> >> --
> >> °cg°
>
> >> Fortune Cookie: A conclusion is simply where you
> >> arrive when you get tired of thinking.
>
> >Name a supposedly independent group that Soros is behind?  Do they
> >leave out money changing when they mention the illls of the country?
>
> But I give you an "A" for the attempt.  The phrase "supposedly
> independent group" is a real gem when it comes to asking loaded
> questions.  But it is moot in any case.
>
> It is your logic and not mine.  My logic says that the problem is the
> system and not liberals vs conservatives.  It has nothing to do with
> supposedly independent groups and everything to do with monied
> interest groups.
>
> As long as folks view the issues as "left vs. right", "dems vs. reps",
> etc. the politicians have what the want - a stable system which is
> littered with corruption and undue influence from all sorts of monied
> groups of which those you name are only some.  
>
> A system that fills politician's pockets and I'll guarantee you that
> they put the money in the most convenient pocket with no regard to the
> pocket  being a left or right pocket..  It is only chump change to the
> unions, corporations of all sorts, AMA, ABA, AARP and on and on.
>
> It isn't even chump change to the drug lords - more like pennies they
> wouldn't bother to bend over to pick up if they saw it laying on the
> sidewalk.
>
> So, your view of influence peddling is going exactly down the road
> that the real interest groups want you to travel.  Question the
> politicians and the so called political groups and you will never see
> those who are pulling the strings (filling the pockets).  Those who
> have an interest in not congressional term limits, reduced campaign
> seasons, increased pay for members of Congress, etc.
>
> Question the system - not the elected players or the supposedly
> independent groups.  Then fix the system.  We will all be the better
> for it.
>
> --
> °cg°
>
> Fortune Cookie: A conclusion is simply where you
> arrive when you get tired of thinking.

Evasion Noted