Ruby_run() w/o exit?

c extending rubyists-

i peeked through ruby.h a little, and of course the pickaxe, but there does
not seem to be a function similar to ruby_run() which does not cause the
calling process to exit(). of course, one can use rb_eval_string(), etc…

what would the best way to repeatedly call the interpreter? is this possible
to do without accumulating global objects/contstants/etc. i feel like i may
be missing some fundemental concept here but i’ve only written one small
extension, and only example programs which embed the interpreter, so my
c<->ruby experience is limited…

thanks in advance.

-a

···

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Hi,

···

At Wed, 14 May 2003 23:43:13 +0900, ahoward wrote:

i peeked through ruby.h a little, and of course the pickaxe, but there does
not seem to be a function similar to ruby_run() which does not cause the
calling process to exit(). of course, one can use rb_eval_string(), etc…

ruby_exec() and ruby_cleanup() in 1.8.


Nobu Nakada

I seem to remember seeing Nobu saying that this feature had been added in
1.8 (quick search…) ah yes, http://ruby-talk.org/63954

Cheers,

Brian.

···

On Wed, May 14, 2003 at 11:43:13PM +0900, ahoward wrote:

c extending rubyists-

i peeked through ruby.h a little, and of course the pickaxe, but there does
not seem to be a function similar to ruby_run() which does not cause the
calling process to exit(). of course, one can use rb_eval_string(), etc…

what would the best way to repeatedly call the interpreter?

rb_funcall()… does most of what you want.
You will have to wrap rb_funcall into a rb_protect() call in order
to catch all exceptions. I you don’t use rb_protect and an error occurs
within your ruby code your application will be exit’ed.

/* ------------------------------
wrap rb_funcall
------------------------------ */
struct Arguments {
VALUE recv;
ID id;
int n;
VALUE *argv;
Arguments(VALUE recv, ID id, int n, VALUE *argv) :
recv(recv), id(id), n(n), argv(argv) {
}
};

VALUE FuncallWrap(VALUE arg) {
Arguments &a = reinterpret_cast<Arguments>(arg);
return rb_funcall2(a.recv, a.id, a.n, a.argv);
}

/*
purpose:
call a ruby function in a safe way.
translate ruby errors into c++ exceptions.
*/
VALUE Funcall(VALUE recv, ID id, int n, …) {
VALUE *argv = 0;

if(n > 0) {
    argv = ALLOCA_N(VALUE, n);
    va_list ar;
    va_start(ar, n);
    int i;
    for(i=0;i<n;i++) {
        argv[i] = va_arg(ar, VALUE);
    }
    va_end(ar);
} 

Arguments arg(recv, id, n, argv);
int error = 0;
VALUE result = rb_protect(FuncallWrap, reinterpret_cast<VALUE>(&arg), &error);
if(error) {
    //throw RubyError::Create("cannot invoke ruby-function");
    throw;
}
return result;

}

VALUE Safe() {
return Funcall(
self,
rb_intern(“my_super_nice_test_function_in_ruby”),
1,
INT2NUM(42)
);
}

See the turorial for furhter info:
http://metaeditor.sourceforge.net/embed/index.html

···

On Wed, 14 May 2003 15:31:14 +0000, ahoward wrote:

i peeked through ruby.h a little, and of course the pickaxe, but there does
not seem to be a function similar to ruby_run() which does not cause the
calling process to exit(). of course, one can use rb_eval_string(), etc…

what would the best way to repeatedly call the interpreter?


Simon Strandgaard

thanks simon, but…

class AHoward
include Singleton
class TooPainFulError < StandardError; end

def initialize
  @langs = %w(ruby c perl java idl bash sh zsh tcsh csh fortran c++)
end

def rank lang
  raise NotImplementedError, lang unless i = @langs.index lang

  case lang
when /[^+]$/
  puts "#{lang} is my ##{i} ranked language"
else
  raise TooPainFulError, lang
  end
end

end

:wink:

-a

···

On Wed, 14 May 2003, Simon Strandgaard wrote:

/*
purpose:
call a ruby function in a safe way.
translate ruby errors into c++ exceptions.
*/

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

I know you asked for C… and that I replyed with C++ :slight_smile:

···


Simon Strandgaard

BTW: I fixed your class so that it works correctly now.

class Howard
class TooPainFulError < StandardError; end

def initialize
@langs = %w(ruby c perl java idl bash sh zsh tcsh csh fortran c++)
end

def rank lang
raise NotImplementedError, lang unless @langs.index lang

case lang

when /[^+]$/
puts "#{lang} is my ##{i} ranked language"
else
raise TooPainFulError, lang
end
end
end

ara = Howard.new
ara.rank “c++”

“Simon Strandgaard” 0bz63fz3m1qt3001@sneakemail.com writes:

I know you asked for C… and that I replyed with C++ :slight_smile:


Simon Strandgaard

BTW: I fixed your class so that it works correctly now.

And now I have fixed the class so that it now works when you rank a
language whose name doesn’t end with plus sign … and I assume that
giving a rank of zero to a language means that it’s the ultimate.

class Howard
class TooPainFulError < StandardError; end

def initialize
  @langs = %w(ruby c perl java idl bash sh zsh tcsh csh fortran c++)
end

def rank lang
  ## Incorrect line replaced:
  ## raise NotImplementedError, lang unless @langs.index lang
  raise NotImplementedError, lang unless (i = @langs.index lang)

  case lang
    when /[^+]$/
      puts "#{lang} is my ##{i} ranked language"
    else
      raise TooPainFulError, lang
  end
end

end

ara = Howard.new
ara.rank “c++”
ara.rank “c”
ara.rank “ruby”

···


Lloyd Zusman
ljz@asfast.com

Hi –

BTW: I fixed your class so that it works correctly now.

Almost :slight_smile:

class Howard
class TooPainFulError < StandardError; end

def initialize
@langs = %w(ruby c perl java idl bash sh zsh tcsh csh fortran c++)
end

def rank lang
raise NotImplementedError, lang unless @langs.index lang

You’ve eliminated the assignment to i, which means that this:

case lang

when /[^+]$/
puts “#{lang} is my ##{i} ranked language”

raises an exception. The original line:

  raise NotImplementedError, lang unless i = @langs.index lang

didn’t parse. Ara, in cases like this you might have to compromise on
the boycotting of parentheses :slight_smile:

raise NotImplementedError, lang unless i = @langs.index(lang)

David

···

On Thu, 15 May 2003, Simon Strandgaard wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

yeah, we’re all programmers here right? :wink:

-a

···

On Thu, 15 May 2003, Lloyd Zusman wrote:

… and I assume that giving a rank of zero to a language means that it’s
the ultimate.

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Hi,

I’m a Ruby newbie, so I was interested in the techniques used in this
thread. At the risk a appearing impertinent, below is the program I
synthesized from the various posts on about this: If nothing else,
it works nicely according to my taste :slight_smile:

dummy = <<“PURPOSE”
call a ruby function in a safe way.
translate ruby errors into c++ exceptions.
PURPOSE

class MyTest
class TooPainFulError < StandardError; end

def initialize
@langs = %w(ruby c perl java idl bash sh zsh tcsh csh fortran c++)
end

def rank lang
l = lang.downcase
raise NotImplementedError, lang unless i = @langs.index(l)
case i
when 0
raise TooPainFulError, lang
else
puts “#{lang} is my ##{i+1} ranked language”
end
end
end

MyTest.new.rank “C”
MyTest.new.rank “NoLang”

···

----- Original Message -----
From: dblack@superlink.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, May 14, 2003 1:30 PM
Subject: Re: ruby_run() w/o exit?

Hi –

On Thu, 15 May 2003, Simon Strandgaard wrote:

BTW: I fixed your class so that it works correctly now.

Almost :slight_smile:

class Howard
class TooPainFulError < StandardError; end

def initialize
@langs = %w(ruby c perl java idl bash sh zsh tcsh csh fortran c++)
end

def rank lang
raise NotImplementedError, lang unless @langs.index lang

You’ve eliminated the assignment to i, which means that this:

case lang

when /[^+]$/
puts “#{lang} is my ##{i} ranked language”

raises an exception. The original line:

  raise NotImplementedError, lang unless i = @langs.index lang

didn’t parse. Ara, in cases like this you might have to compromise on
the boycotting of parentheses :slight_smile:

raise NotImplementedError, lang unless i = @langs.index(lang)

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

sheesh. first matz, now you. poetry moders unite!

i think this was the first (and last!) time i’ll post code without running
it. you guys keep a fella on his toes! :wink:

this is perhaps more appropriate - and it runs:

----CODE----
require ‘singleton’

class Language
class MemoryLeakError < StandardError
attr :lang
def initialize(lang, *a); @lang = lang; super *a; end
end
attr :name
alias to_s name
def initialize name; @name = name; end
end

class AHoward
include Singleton
attr :langs

def initialize
  @langs = %w(ruby c perl java idl bash sh zsh tcsh csh fortran c++)
  @langs.map!{|l| Language.new l.gsub /[+]/, '-'}
end

def rank lang
  raise Language::MemoryLeakError.new lang if /--$/.match lang.name
  raise NotImplementedError unless i = langs.index(lang)
  return i
end

end

ara = AHoward.instance

begin
ara.langs.map{|l| puts “%8.8s : %s” % [l, ara.rank l]}
rescue Language::MemoryLeakError => leak
STDERR.puts “LEAK FROM %s!” % leak.lang
raise
end

----CODE----

~/eg/ruby > ruby ahoward.rb
ruby : 0
c : 1
perl : 2
java : 3
idl : 4
bash : 5
sh : 6
zsh : 7
tcsh : 8
csh : 9
fortran : 10
LEAK FROM c–!
ahoward.rb:23:in rank': Language::MemoryLeakError (Language::MemoryLeakError) from ahoward.rb:32 from ahoward.rb:32:in map’
from ahoward.rb:32:in each' from ahoward.rb:32:in map’
from ahoward.rb:32

-a

···

On Thu, 15 May 2003 dblack@superlink.net wrote:

Ara, in cases like this you might have to compromise on the boycotting of
parentheses :slight_smile:

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ara.t.howard@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

Be careful with that; look what happened to the Vogons.

···

— ahoward ahoward@fsl.noaa.gov wrote:

On Thu, 15 May 2003 dblack@superlink.net wrote:

Ara, in cases like this you might have to compromise on the
boycotting of
parentheses :slight_smile:

sheesh. first matz, now you. poetry moders unite!


Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.