C or C++?

I’d like to start writing Ruby extensions. Does it make a difference
whether you use C or C++? I took a C++ in college but haven’t done any of
it since, so I’m more or less starting from scratch.

Thanks for any input!

Write using the same language and libraries that Ruby itself is written in.

Joe Cheng wrote:

···

I’d like to start writing Ruby extensions. Does it make a difference
whether you use C or C++? I took a C++ in college but haven’t done any of
it since, so I’m more or less starting from scratch.

Thanks for any input!

.

Joe Cheng wrote:

I’d like to start writing Ruby extensions. Does it make a difference
whether you use C or C++? I took a C++ in college but haven’t done any of
it since, so I’m more or less starting from scratch.

Ruby is really C-oriented. It longjmps on errors, so the stack doesn’t
“unwind” the way C++ objects really wants it to. There are work-arounds
for that, but I found that without a good foundation of libraries to
work with, C++ was a real pain, so I stick with C. Plain C is also a
bit more portable than C++.

Sean O'Dell

Why do you want to write extensions? Why cant you write ruby code? If
you want to write and bindings for a library and the library is C++ then
you had better write the binding in C++ otherwise I recommend using C.

or…

I think I saw some benchmarking that said C is faster than C++ so if you
are writing a calculation heavy program then C may be a better option,
IMHO.

Good luck

Paul

···

On Thu, 2003-10-02 at 16:45, Joe Cheng wrote:

I’d like to start writing Ruby extensions. Does it make a difference

Ruby is really C-oriented. It longjmps on errors, so the stack doesn’t
“unwind” the way C++ objects really wants it to.

I think I understand what you mean. Is it that destructors may not be called
as expected? Please elaborate!

There are work-arounds
for that, but I found that without a good foundation of libraries to
work with, C++ was a real pain, so I stick with C.

What kind of work arounds? If you rb_protect everything the longjmp’s should
only happen in ruby-land, and you should be safe in C++, right?

Cheers,

Thomas

“Paul William” maillist@bestworldweb.homelinux.com wrote in message
news:1065092409.545.11.camel@debian…

···

On Thu, 2003-10-02 at 16:45, Joe Cheng wrote:

I’d like to start writing Ruby extensions. Does it make a difference

Why do you want to write extensions? Why cant you write ruby code? If
you want to write and bindings for a library and the library is C++ then
you had better write the binding in C++ otherwise I recommend using C.

I think C is useful for acessing some system resources.
Massive computations should be made with assembler or at least fortran :wink:

Aleksei Guzev

Hi –

···

On Thu, 2 Oct 2003, Paul William wrote:

On Thu, 2003-10-02 at 16:45, Joe Cheng wrote:

I’d like to start writing Ruby extensions. Does it make a difference

Why do you want to write extensions? Why cant you write ruby code?

It’s not an either-or choice; lots of Ruby programmers do both.
C extensions are usually written for speed.

David


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

“Thomas Sondergaard” thomas@FirstNameGoesHereSondergaard.com schrieb im
Newsbeitrag news:3f7bef63$0$48887$edfadb0f@dtext02.news.tele.dk…

Ruby is really C-oriented. It longjmps on errors, so the stack
doesn’t
“unwind” the way C++ objects really wants it to.

I think I understand what you mean. Is it that destructors may not be
called
as expected? Please elaborate!

There are work-arounds
for that, but I found that without a good foundation of libraries to
work with, C++ was a real pain, so I stick with C.

What kind of work arounds? If you rb_protect everything the longjmp’s
should
only happen in ruby-land, and you should be safe in C++, right?

You can only be safe in C++ land if your C++ never invokes Ruby C
functions which might longjump. So, if you just do C++ processing and
return values it’s ok, but if part of your C++ processing is to invoke
methods defined in Ruby (likely for callbacks such as blocks) you can
easily shoot yourself in the foot…

Regards

robert

The biggest problem i have with Ruby is the sleepness
nights… ;->

Has anyone heard of e4x…emcascript(aka javascript)
for xml?

http://dev2dev.bea.com/products/wlworkshop/articles/JSchneider_XML.jsp

Basically it is an attempt to roll xml as a native
type into ecmascript. I’d suggest Ruby could benefit
and set itself apart by doing so. Following is my own
quick and dirty Ruby’d version of the above.(you might
want to check it out first)

xml literal…

p = %x{

paul

john

}
or
p <<<XML

paul


john




XML
#access
mary = p.Peeps.P[3]#xpath starts with 1…

#edit
mary = ‘mary’#convenience method appending text to P
element
#add element
p.Peeps << %x{

Ringo

}

#iter.
p.Peeps.each(‘P’) do |person|

puts person #>paul,john,mary,ringo

end

Instead of the dot notation for swiching context
suggested in the above article, perhaps use xpath
exclusively,imo.

p.each(’//P’) do …end

So, basically, rexml rolled into ruby natively.

But what about the much maligned xsl stylesheets? We
need style too - Rubystyle!

prob a million ways to do this…

style = <<<STYLE

#{P}

is a rock star
STYLE ...here's one output = p.each('//P').transform(style)

There’s plenty of room for rolling more
organic/programic/rubylike approaches to style than
xsl into Ruby(particulary considering Ruby’s iters and
blocks). I’m not sure this is the wisest approach, and
think people like Michael Kay(an xsl guru) might point
out the folly of this. And keeping to a standard would
be good for a product such as Ruby but it sure is
intersting…at least to me.

So now we’ve

  1. done what javascript might be doing but totally
    trumped them with (ruby)style.
  2. added a feature that python and perl can only wish
    for
  3. created an internal common data structure that is
    similar to the array, hash and struct but is sometimes
    better and certainly more portable/readable.
  4. now a native type, we can import/export data to
    whatever speaks xml without worrying about
    implementation.
  5. have an inherent mechanism for seperating duties
    within code- ie data from style
    6)a powerful native templating system. Great for web
    stuff particularily. Picture putting the power of ruby
    in templates that are sandboxed. No need to learn
    template language X. Do it in ruby or subset of.
    6+) prob much more, xml tends to feed upon itself

Whatcha think? Can i count on this by 1.8.1? ;->

:pv

PS:as for the previous comment at artima about the
limitations of Ruby - stone that guy! :slight_smile: Not really-
seemed like legit points 2me. I want Ruby to scale to
1 mil hits per sec on my ipaq too - but not at the
expense of creativity, malability, fidelity,
pragmatism or effectiveness.

···

Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
http://shopping.yahoo.com

Hi –

···

On Thu, 2 Oct 2003 dblack@superlink.net wrote:

On Thu, 2 Oct 2003, Paul William wrote:

> On Thu, 2003-10-02 at 16:45, Joe Cheng wrote:

> > I’d like to start writing Ruby extensions. Does it make a difference

>

> Why do you want to write extensions? Why cant you write ruby code?

It’s not an either-or choice; lots of Ruby programmers do both.

C extensions are usually written for speed.

…or to integrate with C-based libraries, which I’d say is one of the
best reasons to use C in your Ruby development. My process would be:

  • If it requires a C library, evaluate how difficult it would be to
    obviate the need for the C library by rewriting the part I need in Ruby (I
    don’t necessarily recommend this, but it’s how I am).
  • If it requires a C library, wrap the C library with a small Ruby
    extension.
  • If it doesn’t require a C library, try pure Ruby before jumping into a
    C extension based on the assumption that C is necessary for speed.
  • If the ruby version is too slow, try to optimize it in Ruby.
  • If it’s not going to be fast enough in Ruby, rewrite the required bits
    in C as an extension.

I’m very C-averse I guess.

Chad

Thomas Sondergaard wrote:

Ruby is really C-oriented. It longjmps on errors, so the stack doesn’t
“unwind” the way C++ objects really wants it to.

I think I understand what you mean. Is it that destructors may not be called
as expected? Please elaborate!

Destructors don’t get called, correct.

There are work-arounds
for that, but I found that without a good foundation of libraries to
work with, C++ was a real pain, so I stick with C.

What kind of work arounds? If you rb_protect everything the longjmp’s should
only happen in ruby-land, and you should be safe in C++, right?

I believe, at least under Windows, that rb_protect didn’t protect me
quite as well as I needed (I still got longjmp’d past), so I abandonded
C++ entirely. I’m a devoted C++ user, so I didn’t give it up easily,
believe me. It was just FAR more trouble trying to be Mr. C++ around
the Ruby engine than it was using C. C isn’t so bad if you got your
head on for it.

Sean O'Dell

Basically it is an attempt to roll xml as a native

type into ecmascript. I’d suggest Ruby could benefit

and set itself apart by doing so. Following is my own

quick and dirty Ruby’d version of the above.(you might

want to check it out first)

···

On Thu, 2 Oct 2003, paul vudmaska wrote:

Pretty much everything you’ve mentioned here is possible with REXML
already. The only thing I can see that isn’t possible is the built-in XML
syntax. I can’t see how hard-wiring XML as a part of Ruby’s syntax
provides much of a benefit at all. It would be fashionable (though not
really all that fashionable these days), but barring that it’s just more
baggage to carry around.

There’s plenty of room for rolling more

organic/programic/rubylike approaches to style than

xsl into Ruby(particulary considering Ruby’s iters and

blocks). I’m not sure this is the wisest approach, and

think people like Michael Kay(an xsl guru) might point

out the folly of this. And keeping to a standard would

be good for a product such as Ruby but it sure is

intersting…at least to me.

Interesting in the same way that Lojban is interesting
(http://www.lojban.org). :slight_smile:

So now we’ve

1) done what javascript might be doing but totally

trumped them with (ruby)style.

2) added a feature that python and perl can only wish

for

Are we playing sports or writing programming languages? I didn’t know it
was a competition. Who are the enemies? Is anyone on our team?

3) created an internal common data structure that is

similar to the array, hash and struct but is sometimes

better and certainly more portable/readable.

more readable?
blahblah2lkjsdf
vs.
[“blah”, “blah2”, “lkjsdf”]

4) now a native type, we can import/export data to

whatever speaks xml without worrying about

implementation.

With REXML in the distribution you can already do this. Just requires a
few more keystrokes.

5) have an inherent mechanism for seperating duties

within code- ie data from style

But you’ve now joined a data representation format to a language. Where
would you rather have coupling?

Whatcha think? Can i count on this by 1.8.1? ;->

No offense intended, but I hope not :slight_smile:

Last year I made a quick hack that could do something like that.
I didn’t have much experience with Ruby back then, so it is a quick and
very dirty solution. But the following works:

require ‘XML’

y = XML %{


Joe
28

Engineering



Ken
26

Engineering



}

puts y.employees.employee[0].age
y.employees.employee[0].age = '9’
puts y.employees.employee[0].age

y.employees.employee.each do |employee|
puts "name: #{employee.name}, age: #{employee.age}"
end

doc = XML %{


I. Wannabuy

53 Party Lane
Anywhere
CA
12345



Large Purple Dinosaur,
Generic
35
24.99


Catapult
1
149.95


300 foot measuring tape
1
9.95


}

def calc_total(order)
total = 0
order.item.each do |item|
total += item.price.to_f * item.quantity.to_i
end
return total
end

puts “total: #{calc_total(doc.order)}”

···

======================================================

This is the file XML.rb that makes it work:

======================================================

require 'xmltreebuilder’
require ‘perlvalue’

ELEMENT_NODE = 1; ATTRIBUTE_NODE = 2; TEXT_NODE = 3

module Kernel
def XML(xml)
RNX.new(xml)
end
end

class RNX
def initialize(xml)
@level = 0; @line = @source = ‘’; @path = []
builder = XML::DOM::Builder.new
begin
dom = builder.parse xml.gsub(/\s+/, ‘’)
rescue
puts “#{$0}: #{$!} (in line #{builder.line}”;
exit 1
end
dom.documentElement.normalize
traverse dom
eval %{
@#{dom.documentElement.nodeName} = PerlValue.new
#{@source}

                     def self.#{dom.documentElement.nodeName}
                             return @#{dom.documentElement.nodeName}
                     end
             }
     end

     def traverse(node)
             index = 0
             node.childNodes.each do |n|
                     if n.nodeType == ELEMENT_NODE
                             @path[@level] = n.nodeName
                             if (n.nextSibling and 

n.nextSibling.nodeName == n.nodeName) or (n.previousSibling and
n.previousSibling.nodeName == n.nodeName)
@path[@level] += "[#{index}]“
index += 1
end
@line = @path.join(’.’)
@level += 1; traverse n; @level -= 1
@path.delete_at(@level)
elsif n.nodeType == TEXT_NODE
@line += " = ‘#{n.data}’\n” if n.data
=~ /\S/ #optional!!!
@source += "@#{@line}"
end
end
end
end

I believe, at least under Windows, that rb_protect didn't protect me
quite as well as I needed (I still got longjmp'd past), so I abandonded
C++ entirely.

With C++, never try to mix 'try {} catch {}' with rb_protect() even with C
be carefull. For example, look at plruby to see how it made the call to
rb_protect() with a setjmp()/longjmp() need by postgres

static VALUE
pl_protect(plth)
    struct pl_thread_st *plth;
{
    Datum retval;
    VALUE result;

    if (sigsetjmp(Warn_restart, 1) != 0) {
  return pl_eCatch;
    }
/* ... */
}

static VALUE
pl_real_handler(struct pl_thread_st *plth)
{
    VALUE result;
    int state;
/* ... */
    state = 0;
    pl_call_level++;
    result = rb_protect(pl_protect, (VALUE)plth, &state);
    pl_call_level--;
/* ... */
}

the order is really important : after this I can safely use rb_raise() in
plruby and I can catch the error given by postgres (when it use longjmp())

Guy Decoux


“Chad Fowler” chad@chadfowler.com
Pretty much everything you’ve mentioned here is
possible with REXML
--------------------<<
Except the style. Besides, that was part of my point.


“Chad Fowler” chad@chadfowler.com
I can’t see how hard-wiring XML as a part of Ruby’s
syntax
provides much of a benefit at all. It would be
fashionable (though not
really all that fashionable these days), but barring
that it’s just
more
baggage to carry around.
---------<<
It’s interesting to me that you would wrap an xml
file(rss) in an object
http://www.chadfowler.com/ruby/rss/
but belittle an offhanded attempt to make inherent
those attributes that you so painstakingly crafted
code for and call the former more baggage. Help me
understand that, Chad!


“Chad Fowler” chad@chadfowler.com

Interesting in the same way that Lojban is interesting

(http://www.lojban.org). :slight_smile:
--------------------<<
for you!


“Chad Fowler” chad@chadfowler.com

Are we playing sports or writing programming
languages? I didn’t know
it
was a competition. Who are the enemies? Is anyone on
our team?
--------------------<<

I’m just a simple scriptor making an open ended
suggestion about a language that i enjoy in a forum
that i would hope would encourage such things - even
if they are stupid. What language are you writing,
btw? Just an analogy, maybe a bad one. Sorry.


“Chad Fowler” chad@chadfowler.com
more readable?

…blahblah2lkjsdf

vs.
[“blah”, “blah2”, “lkjsdf”]
--------------------<<

More readable as in ‘more context’ not as in more
succinct or fewer letters or anything else.(ie are
those an array of items in a list or just a lot of
blah?)


With REXML in the distribution you can already do
this. Just requires a few more keystrokes.
-----<
Sure but fewer works for me better.


“Chad Fowler” chad@chadfowler.com
Where
would you rather have coupling?
-----------<<
Where it serves me best. I was merely contemplating
another tool to choose from. I’m not unaware of the
implications.


“Chad Fowler” chad@chadfowler.com
No offense intended, but I hope not :slight_smile:
-----------<<

None taken!


“Erik Terpstra” erik@terpnet.nl
Last year I made a quick hack that could do something
like that.
I didn’t have much experience with Ruby back then, so
it is a quick and
very dirty solution. But the following works:
---------->

Thanks Erik i’m going to take a look at this and see
if i can use it! I’m still a newbie and will prob
learn a lot from it in any case.

:stuck_out_tongue:

···

Do you Yahoo!?
The New Yahoo! Shopping - with improved product search

I think it would be wonderful if Ruby could handle XML somewhat how Flash
Actionscript handles XML( but with a few tweaks here and there of course ),
but the overall way that the Actionscript XML Object is a beautiful thing.
Or at least I think. I think it would be a plus.

-Zach

>>--------

“Chad Fowler” chad@chadfowler.com

Pretty much everything you’ve mentioned here is

possible with REXML

--------------------<<

Except the style. Besides, that was part of my point.

···

On Fri, 3 Oct 2003, paul vudmaska wrote:

Well, when you say “style” I think you mainly mean syntax, right? Other
than XML being first-class syntax, the rest of the style you’ve proposed
is do-able (and reflected quite well in Erik’s posted code).

>>--------

“Chad Fowler” chad@chadfowler.com

I can’t see how hard-wiring XML as a part of Ruby’s

syntax

provides much of a benefit at all. It would be

fashionable (though not

really all that fashionable these days), but barring

that it’s just

more

baggage to carry around.

---------<<

It’s interesting to me that you would wrap an xml

file(rss) in an object

http://www.chadfowler.com/ruby/rss/

but belittle an offhanded attempt to make inherent

those attributes that you so painstakingly crafted

code for and call the former more baggage. Help me

understand that, Chad!

First of all, you could hardly call the process of creating that simple
library painstaking. The fact that I wrapped RSS parsing in an object
demonstrates (though rather poorly considering the age of this code–see
Austin Ziegler’s replacement on SourceForge) my belief that things like this
should be abstracted into libraries and not the language. Someone else has
created a CSV library. We could include CSV as a native data type too.
How about URIs? We have uri.rb, but why shouldn’t we be able to simply
say:

myURI = http://www.ruby-lang.org

How about JPEG files? With the right graphical programming environment,
you could actually embed pictures into code. myVar = . myPic.vertical_flip.

>>--------

“Chad Fowler” chad@chadfowler.com

Interesting in the same way that Lojban is interesting

(http://www.lojban.org). :slight_smile:

--------------------<<

for you!

Don’t you find Lojban interesting?

>>--------

“Chad Fowler” chad@chadfowler.com

Are we playing sports or writing programming

languages? I didn’t know

it

was a competition. Who are the enemies? Is anyone on

our team?

--------------------<<

I’m just a simple scriptor making an open ended

suggestion about a language that i enjoy in a forum

that i would hope would encourage such things - even

if they are stupid. What language are you writing,

btw? Just an analogy, maybe a bad one. Sorry.

“We” as in the community, of course. And the language would be ruby. I
certainly wasn’t implying that I’m personally creating Ruby, but that’s
pretty obvious. I was serious about the question I asked, though. Why
the competition?

>>--------

“Erik Terpstra” erik@terpnet.nl

Last year I made a quick hack that could do something

like that.

I didn’t have much experience with Ruby back then, so

it is a quick and

very dirty solution. But the following works:

---------->

Erik demonstrated the same point I was trying to make, but he had some
working code :slight_smile:

Hi –

Chad wrote:

I can’t see how hard-wiring XML as a part of Ruby’s syntax
provides much of a benefit at all. It would be fashionable
(though not really all that fashionable these days), but barring
that it’s just more baggage to carry around.

It’s interesting to me that you would wrap an xml file(rss) in an
object http://www.chadfowler.com/ruby/rss/ but belittle an offhanded
attempt to make inherent those attributes that you so painstakingly
crafted code for and call the former more baggage. Help me
understand that, Chad!

That’s how it works: there’s a core language, and then there are
libraries and programs. You can’t put everything in the core
language.

more readable?

…blahblah2lkjsdf

vs.
[“blah”, “blah2”, “lkjsdf”]

More readable as in ‘more context’ not as in more succinct or fewer
letters or anything else.(ie are those an array of items in a list
or just a lot of blah?)

It’s just a lot of blah – well, an array of strings, to be a little
more specific. They may or may not serve as “items” in a “list” at
some point, inside some kind of object and/or markup system. That’s a
separate matter.

David

···

On Fri, 3 Oct 2003, paul vudmaska wrote:


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

“Chad Fowler” chad@chadfowler.com

I can’t see how hard-wiring XML as a part of Ruby’s syntax
provides much of a benefit at all. It would be fashionable
(though not really all that fashionable these days), but barring
that it’s just more baggage to carry around.
It’s interesting to me that you would wrap an xml file(rss) in an
object http://www.chadfowler.com/ruby/rss/ but belittle an
offhanded attempt to make inherent those attributes that you so
painstakingly crafted code for and call the former more baggage.
Help me understand that, Chad!

I’m not Chad, but as someone who liked what Chad did and wrote a
follow-up library that (mostly) conformed to his API, I can explain
exactly why I did it much the same way. Basically, Chad is
representing an object (a rich site summary, or a channel). That it
happens to correspond to an XML format is somewhat irrelevant. There
are actually four common variants on this, and two less common
variants. By designing channels as objects, it becomes relatively
easy to support those variant formats.

more readable?
blahblah2lkjsdf
vs.
[“blah”, “blah2”, “lkjsdf”]
More readable as in ‘more context’ not as in more succinct or
fewer letters or anything else. (ie are those an array of items in
a list or just a lot of blah?)

There is no meaningful distinction between an array and a list in
Ruby. In all likelihood, you might do:

class List
def initialize(*items)
@items = *items
end
attr_reader :items
end

list = List.new(%w{blah blah2 ljksdf})
list.items # => [“blah”, “blah2”, “lkjsdf”]

When you’re writing a program, why would you want to add unnecessary
semantic information (e.g., encode the list as XML) when the
necessary semantic information is already present. The only time you
need to worry about such semantic information is when you write it
to an external format … like XML.

With REXML in the distribution you can already do this. Just
requires a few more keystrokes.
Sure but fewer works for me better.

Yeah, but you’ve traded fewer keystrokes here in favour of more
keystrokes elsewhere.

You may want to look around the archives for a discussion of %y{}
(for YAML) and the like that happened around the 10th of September.
Basically, some of the same suggestions you’ve made came up – and
weren’t really liked.

-austin

···

On Fri, 3 Oct 2003 01:46:38 +0900, paul vudmaska wrote:

austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.10.02
* 13.58.40

I think it would be wonderful if Ruby could handle XML somewhat how Flash

Actionscript handles XML( but with a few tweaks here and there of course ),

but the overall way that the Actionscript XML Object is a beautiful thing.

Or at least I think. I think it would be a plus.

···

On Fri, 3 Oct 2003, Zach Dennis wrote:

Zach, do you have a link or an example? I’ve heard good things about
ActionScript’s XML handling myself.

Chad