Read the latest web development and design tips at Fred Wu's new blog! :-)
Poke me on GitHub

Archive for the ‘Ruby’ Category

Using Ruby MySQL Gem with MAMP 1.8.x on Snow Leopard

Being primarily a PHP developer, I always use an AMP package no matter what development platform I am on. So when it comes to developing Ruby/Rails applications on the same platform, I’d like to use what is already available.

Mike Boone has posted a very useful tutorial on how to get MySQL gem and MAMP 1.7.2 up and running. So, to recap and make the tutorial compatible with Snow Leopard and MAMP 1.8.x, here is what you need to do:

  1. Download the latest MAMP dmg file.
  2. Download the 1.8.2 (or whichever the latest one you could find) components file from this page.
  3. Unzip, mount the dmg, then copy the MySQL source file (mysql-5.1.37.tar.gz) to somewhere on your hard drive.
  4. Untar the MySQL source file, and `cd` to the source file directory.
  5. Compile the library:


    $ ./configure --with-unix-socket-path=/Applications/MAMP/tmp/mysql/mysql.sock --without-server --prefix=/Applications/MAMP/Library

    $ make -j2

  6. Copy the compiled libraries into MAMP:

    $ cp libmysql/.libs/*.dylib /Applications/MAMP/Library/lib/mysql

  7. Copy the MYSQL headers into MAMP:


    $ mkdir /Applications/MAMP/Library/include

    $ cp -R include /Applications/MAMP/Library/include/mysql

  8. Install the Ruby MySQL Gem, on Snow Leopard:


    $ sudo env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/Applications/MAMP/Library/bin/mysql_config

    On Leopard:


    $ sudo env ARCHFLAGS="-arch i386" gem install mysql -- --with-mysql-config=/Applications/MAMP/Library/bin/mysql_config

Enjoy!

EDIT @ 2009-11-23: If you’re still experiencing problems (perhaps with RVM), try adding “/Applications/MAMP/Library/bin/” to your $PATH in “~/.bash_profile”.

Related posts

Comparison: Ruby vs PHP, the Pros

Just to be clear, this is NOT a fanboyism post nor do I encourage the debate between which language is superior. My personal belief is to use the appropriate tool for appropriate projects.

PHP Pros

  • Widely available libraries (including PEAR and PECL)
  • An absolutely outstanding online manual
  • Relatively easy to learn (can hack together some code without deep understanding of the language)
  • Widely used (easy to find clients, etc)
  • Most if not all control panels have PHP integration
  • Development packages (XAMPP, WAMP, MAMP, LAMP, etc)
  • C style syntax (easier transition for people with C/C++/Java background)
  • Powerful array feature (Ruby’s equivalent to PHP’s associative array is Hash, which is not interchangeable with array)
  • PhpDoc is better than RDoc (this might be because I am too used to PhpDoc)

Ruby Pros

  • Fully object oriented
  • Rake (Ruby’s Make)
  • RubyGem (Ruby’s apt/yum)
  • Code blocks
  • ‘Ghost’ classes
  • Modules (aka namespace/package, PHP 5.3 will have namespace too, but with much uglier syntax)
  • lambda functions (PHP 5.3 will have this too, but not as powerful)
  • Children-aware parent classes (‘inherited’ hook)
  • Multi-inheritance through Mixins (PHP is single-inheritance)
  • Ruby 1.9 is unicode friendly (PHP 6 will be)
  • For what I do, Ruby on Rails > all PHP frameworks combined
  • Regular Expression built into the language core

I am pretty sure there’s heaps of pros for both PHP and Ruby missing from the list, but at least it gives you a rough idea on what to look for if you are not very familiar with them.

Please share your experience with us too (and I will update this post accordingly). :)

Related posts

Variable methods and accessors in Ruby

Yeah I am a lame PHP guy who hasn’t gotten too deeply into Ruby yet. ;)

In PHP I often use variable methods, for example:

$foo = new Foo();

$funcname = 'dynamic_method';
$foo->$funcname();
// Same as calling $foo->dynamic_method();

$varname = 'dynamic_accessor';
$foo->$varname = 'some value';
// Same as calling $foo->dynamic_accessor = 'some value';

Now, because Ruby does not prefix $ in front of variables, it is impossible to use variable methods the way we do in PHP.

I am sure for Ruby gurus it’s pretty obvious but for me, I just spent more than 30 minutes searching for an alternative other than evil eval, and I finally found one.

We use the PHP call_user_func and call_user_func_array equivalent in Ruby: send or __send__.

Luckily accessors in Ruby are methods, so we are able to use the send method for both methods and accessors.

For example we can set a variable accessor like this:

foo = Foo.new

funcname = 'dynamic_method'
foo.send "#{funcname}"
# same as calling foo.dynamic_method

varname = 'dynamic_accessor'
foo.send "#{varname}=", 'some value'
# same as calling foo.dynamic_accessor = 'some value'

I wish in future versions of Ruby, we can somehow assign accessor values the way we do in PHP. :)

Related posts

Ruby on Rails, Passenger (ModRails) vs CodeIgniter and Kohana

Disclaimer: This is a very simple, ‘Hello World’ benchmark which has no impact to any real world applications. A more thorough benchmark test (by building two real world applications) is planned. :)

Disclaimer 2:I apologise for posting such a useless benchmark (I certainly didn’t expect it to hit the DZone front page), but I think most of you missed the point. I merely posted this as a result of surprise (to me anyway). At a later stage I will conduct a much more meaningful comparison between some of the frameworks. Until then, please ignore this post. :)

Last few days I have been playing with Ruby and Rails, again.

Today, when someone was asking on a forum about the efficiency of web frameworks, I thought I’d give the few frameworks I work with some more benchmark testing.

So I went ahead and benchmarked CodeIgniter, Kohana and Rails, using a simple ‘Hello World!’ page. Now before I post any benchmark results, you should know that I have previously done a benchmark test on CodeIgniter, Kohana and CakePHP. CodeIgniter and Kohana shared similar results.

(more…)

Related posts