Regex to transform java code to ecma script

Hi,

I was hoping someone could provide me with a better way to accomplish
(preferrably in one line) the following simple task:

typical method declaration in a java interface

code = “public void myFunction(String foo);”

needs to be transformed into the following e.g. Actionscript 2.0

(ECMA)

public function myFunction(foo:String):Void;

java = code.gsub(/(public) (.?) (.).*(;)/) { $1 + ’ function ’ + $3

  • ‘:’ + $2.capitalize + “;” }
    puts code.gsub(/((String) (foo))/,’(\2:\1)’)

It works, but it’s not very slick…

Thanks!

Peder :slight_smile:

Sorry, my mistake…cut and paste is dangerous. The code should read:

code = “public void myFunction(String foo);”

code = code.gsub(/(public) (.?) (.).*(;)/) { $1 + ’ function ’ + $3

  • ‘:’ + $2.capitalize + “;” }

puts code.gsub(/((String) (foo))/,’(\2:\1)’)

Thanks again,

Peder :slight_smile:

pondus wrote:

public function myFunction(foo:String):Void;

java = code.gsub(/(public) (.?) (.).*(;)/) { $1 + ’ function ’ + $3

  • ‘:’ + $2.capitalize + “;” }
    puts code.gsub(/((String) (foo))/,’(\2:\1)’)

If you can convert Java to ActionScript with a one-line regex, that’s
pretty good.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs