I'm writing an application in ruby and would like to access some legacy code written in another language (php). Unfortunately this legacy code does not have an HTTP API, but it is living on the same file system. I had the idea that maybe instead of building an API, or rewriting all of the code in ruby, there may be some clever way I can expose these PHP functions so ruby can access them. Do you have an idea or approach I can use to accomplish this?
2 Answers
Assuming you mean code that is callable at the command-line, using either backticks or the %x
operator or the more complex system
, popen
and popen3
commands lets you execute separate pieces of code and gather the results.
For instance in IRB:
irb(main):002:0> puts `ls`
Desktop
Documents
Wrap that in a method and it becomes a way to call an external program:
def ls(s='')
`ls #{s}`
end
ls() # => "Desktop\nDocuments\nDownloads\nLibrary\nMovies\nMusic\nPictures\nPublic\nSites\nVirtualBox VMs\nbin\ndevelopment\nlibsmi\nperl5\nproduction\nshare\ntest.data\ntmp\n"
ls('M*') # => "Movies:\n\nMusic:\niTunes\n"
See "Ruby, Difference between exec, system and %x() or Backticks" for additional information.

- 1
- 1

- 158,662
- 42
- 215
- 303
The only reasonable solution I can think of is to rewrite chunks of the php or to write an http api.
If you want to embark upon a more adventurous route you could take a look compiling the php into exes (Convert a PHP script into a stand-alone windows executable) and then execute them via backticks, system, or etc...
It probably would be best to rewrite it though.

- 1
- 1

- 464
- 5
- 12