Ruby: May 2006 Archives

After almost a year, I'm pleased to announce that another beta of ruby2c is available. The project stagnated for a bit, but is coming back in force! We've solidified this release by gemifying it, splitting up our tail end translator to convert to either ANSI C or ruby internals C, and making it much more happy with other tool-chains (like our obfuscator).

ruby2c translates a static ruby subset to C. Hopefully it works.

Enough Propaganda! Just install it!

sudo gem install RubyToC

CHANGES:

+ 6 minor enhancements:
+ Split RubyToC to RubyToRubyC and RubyToAnsiC.
+ Extended Environment to be more flexible for various situations.
+ Removed propaganda (bloat) from release.
+ Gemified and reorganized things. Support still needs splitting up.
+ Flipped a lot of internal naming to use Unique.
+ Added ruby_to_c_show (like parse_tree_show).

+ 4(ish) bug fixes:
+ Use ivars instead of cvars so inheritance won't bugger the translator.
+ Corrected unsupported node lists in pipeline.
+ Fixed bugs for splat args, iters, optional args, method name map.
+ Fixed many other bugs.

13:58:29 srbaker : i've had half a dozen folks look over this, including michael granger
13:58:42 srbaker : i've stripped it down to the bare basics: ie: so simple it can't be wrong.
13:59:04 zenspider : "so simple it can't be wrong" can I quote you on that???
13:59:27 srbaker : oh god no.

Phase 1: All the unit tests are passing.

Phase 2: We can even obfuscate the unit tests and run the binary version of them and they still pass.

Phase 3: We're currently working on obfuscating the obfuscator itself and passing the unit tests a third time.

We've got 6 methods (out of 56) not translating. 2 of them I think I can do w/o too much hassle, but the other 4 (all 1 related issue) are a PITA (closure semantics just don't map to C very well). I think I just brainstormed a solution that may actually work once I have rested my brain some.

One nice thing is that every lesson we learn from the obfuscator is something we can potentially use to improve ruby2c.

That said, we're damn close to having a full-fledged ruby obfuscator that we'll make commercially available in the near future.

Original Request:

I'd like to sort an array of strings numerically...

Modifications:

... but that doesn't handle letters (non-number strings)
... mine was shorter, but it uses an intermediate variable
... nice, but how do you get the digits before the letters?

My final version:

%w(z A 1 10 2 b).sort_by { |s| [ s[0], s.to_i, s ] }

(ugh)

Update:

kodis did a good job pointing out that I shouldn't blog at 2am. The above code is buggy. Namely, the first term in the sort_by is flat out wrong and was a quick add right at the end that I didn't do a good job of testing. So, the elegant version is:

%w(z A 1 10 2 b).sort_by { |s| [ s.to_i, s ] }

but the one that will sort numbers first (no idea why still, but hey) is:

%w(z A 1 10 2 b).sort_by { |s| [ s =~ /^\d/ ? 1 : 2, s.to_i, s ] }

still, even that ugly sort_by is vastly better than my first attempt:

%w(z a 1 10 2 b).map {|o| Integer(o) rescue o }.partition { |o|
  Fixnum === o }.map { |a| a.sort }.flatten.map { |o| o.to_s }

blech!

About this Archive

This page is a archive of entries in the Ruby category from May 2006.

Ruby: April 2006 is the previous archive.

Ruby: June 2006 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Pages

Powered by Movable Type 4.1