PHP-to-Ruby Rails Plugin

December 29th, 2006

Aight, the alpha-version of my php/rails plugin is up. There is a ton of work left to do on it. I’ve yet to compile the various snippets that I have scattered amongst my projects, but this should get some aspiring neophyte php programmers into switching to the non-Dark Side of web development.

Install it with


./script/plugin install svn://ahgsoftware.com/php_compat/trunk

And here are some examples

1
2
3
4
5
6
7
8
9
10
# Dates
PhpCompat.date("F j, Y, g:i")   # Translated to Time.strftime chars
PhpCompat.time  # Integer unix timestamp

# Posix methods
PhpCompat.get_pwnam('localusername') # Struct with passwd info

# File methods (not network enabled... yet!)
PhpCompat.file_put_contents('/tmp/file.txt',"yo yo yo")
PhpCompat.file_get_contents('/tmp/file.txt')

A few caveats..

  1. The php date-format to ruby Time-strftime translations are about 90% complete. There aren’t any convenient fallbacks, so certain formatting characters will not translate at all.
  2. The posix functions are POSIX functions!! So don’t expect much luck on Windows, although, they work great on OS X (naturally ;) )
  3. File methods don’t work on url’s yet
  4. There are some functions that I’ll never translate, as some things you just HAVE to do the ruby way. Its so much more concise, readable, and effective when you do it the ‘matz way’.

Since the main focus of this plugin is to make software migrations a bit less painful, the next set of methods that I wanna stick in there, is the mysql functions. I’ve used this in migrating thousands of lines of php code to ruby, so, I hope it helps someone else out too!

2 Responses to “PHP-to-Ruby Rails Plugin”

  1. Oleg Andreev Says:
    Maybe it's better to write a simple translator to port PHP code to Ruby?
  2. Michael Cerna Says:
    I've thought about doing that. This is kind of a loaded plugin in that its supposed to be pseudo-pedagogical for PHP to Rails adopters, still be useful to anyone, and not desecrate the principles on which my community bases its love of Ruby. I'd like to allow PHP programmers (the lazy ones, not the ones who can learn Rails adeptly in a short time!) to use some of their favorite functions, but, to also not have access to some functions that would better be done the Rails way. As a transitory plugin, it should (hopefully) inspire them to learn about blocks, closures, and not have their budding projects stop because they have to convert TONS of date()'s, or they have 30 mailouts scattered throughout their code that they must convert to TMail or ActionMailer! I think the lexical complexity of parsing and converting to ruby (or a php->ruby interpreter) would require too much setup to allow easy deployment, and it wouldn't teach the nubes anything (I'm probably still half-nube myself)!

Leave a Reply

 

Michael Cerna Chicago-based Rails Developer and Avid Musician. More ...

Search

Categories

  • Home (15)
  • Rails Plugins (5)
  • Pages (9)
  • Archives

    Tags

    BlogRoll