exec, system, and “
Haven’t used this since 2007 and completely forgot. Yes, I use this blog as a notebook.
exec(“ls”) => exits from irb or the program
system(“ls”) => returns true or false (crap out)
`ls` => returns the output of the command as a string -> Nice!
Introduction to MongoDB – Part III, MongoMapper & Rails
This is the third and final episode on introduction to MongDB. In this screencast, we demonstrate MongoMapper and how to use it in a Rails app. Get the code at http://blog.rubyhead.com/files/mongodemo.zip
Introduction to MongoDB – Part III, MongoMapper & Rails.
Introduction to MongoDB – Part II, Ruby
This is the second part of the series on MongoDB. In this screencast, we create a small Sinatra app called YASI that accesses the data using MongoDB. Get the code at http://blog.rubyhead.com/files/yasi.zip
Introduction to MongoDB – Part II, Ruby
Introduction to MongoDB – Part I
Here it is, the first of a series on MongoDB. I decided not to go over the concepts since there are extensive coverage already on the net. I highly recommend the presentation slides from nyc.rb and also checkout Peepcode’s screencast on CouchDB.
Introduction to MongoDB – Part I from Joon You on Vimeo.
Ubuntu 9.10 & Ruby – Installation
Being forced to stay in due to falling off the roof, here’s how I got the Ruby working correctly on new Ubuntu 9.10.
Amazingly, the old sudo apt-get install from my Hardy Heron post still works well. I tried doing sudo apt-get install ruby-full, but it installed a bunch of garbage that relates to emacs which I don’t use.
~$ sudo apt-get install ruby irb ri rdoc ruby1.8-dev libzlib-ruby libyaml-ruby libreadline-ruby libncurses-ruby libcurses-ruby libruby libruby-extras libfcgi-ruby1.8 build-essential libopenssl-ruby libdbm-ruby libdbi-ruby libdbd-sqlite3-ruby sqlite3 libsqlite3-dev libsqlite3-ruby libxml-ruby libxml2-dev
It still holds true that you have to install rubygems from the source, NOT the repository. Why? Rubygems should not be installed using the apt-get again due to the fact that it will not let you do gem update –system. This can lead to future upgrade later on. Therefore, download and install from Rubyforge.
wget http://rubyforge.org/frs/download.php/60718/rubygems-1.3.5.tgz
tar xvzf rubygems-1.3.5.tgz
cd rubygems-1.3.5
sudo ruby setup.rb
sudo ln -s /usr/bin/gem1.8 /usr/bin/gem
And for those of you still using MySQL, this changed just a slight bit.
sudo apt-get install mysql-server mysql-client libdbd-mysql-ruby libmysqlclient-dev
sudo gem install mysql
The Ruby is version 1.8.7, and if you want the true 1.8.6 version running, you should also install it with the old versions of the library. I found that even with 1.8.6, you still get 1.8.7 problems due to libraries, especially the bindings, but YMMV.
By the way, there’s a bug in 9.10 with gvim where you get stupid gtk gravity failure notice whenever you start gvim from the command line. Although it’s harmless, I find it extremely annoying since it is my editor.
Rack’s env
Ever wonder what the “env” looked like in Rack? Run this code via Ruby and point your browser to http://localhost:4000 to find out.
class SuperMicroApp
def call(env)
[200, {'Content-Type' => 'text/html'}, pretty_env(env) ]
end
def pretty_env(hash)
pretty_hash = ""
hash.each do |k,v|
pretty_hash += "#{k}: #{v} --> #{v.class}<br />"
end
pretty_hash += "<br /><br />"
hash["REQUEST_PATH"].split("/").each do |r|
pretty_hash += "#{r=="" ? "none" : r}<br />"
end
return pretty_hash
end
end
require 'rubygems'
require 'rack'
Rack::Handler::Thin.run(SuperMicroApp.new, :Port => 4000)
Dynamically Instantiating an Object
Ever wanted to write a method that instantiated an object of a class you specify? Below should be self-explanatory. Just call “const_get” method on the Object.
>> a = Object.const_get("String").new
=> ""
>> a.class
=> String
>> class Joon
>> def initialize
>> puts "Hello Joon"
>> end
>> end
=> nil
>> a = Object.const_get("Joon").new
Hello Joon
=> #
>>
MacRuby Learning
As a former Obj-C+Cocoa developer, I can’t tell you how happy I am about MacRuby. To be quite honest, after taking a brief look at RubyCocoa, I wasn’t too excited. But after taking a careful look at MacRuby, I’m very excited. In fact, I think this is as big as Rails for Ruby.
Here’s a list of learning resources I compiled:
Pragmatic Studio Bonus Track #6 and #7 – screencasts
Mike Clark did an excellent job explaining MacRuby. I’m very stunned that he’s not charging for these two screencasts. Who know, he may come to his senses and start charging for them. Get them now!
Meet MacRuby by Peepcode – screencast
I already reviewed this. It’s $9 and well worth it.
Developing Cocoa Application Using MacRuby
Excellent tutorial from Apple. Basically similar tutorial as Mike Clark’s screencast. I wonder if he wrote this…
MacRuby.org
The official site with documentation and all the goodness.
By the way, to my delight, MacRuby is based on 1.9 version of Ruby. Have fun!
RubyHead Screencast Short – Subdomain in Rails
Ever wonder how Rails apps can recognize your account by subdomains? Well, here’s how.
RubyHead Short – Subdomain in Rails from Joon You on Vimeo.
Freezing Libxml-Ruby Gem In a Rails App
There are times when you want to freeze a gem to make the deployment easier. As for me, I wanted to make sure that my app is using 1.1.3 version of libxml gem. This is a bit of problem since it requires a compilation.
I made this easy. All I have to do is download this file, extract to vendor/gems directory, and make sure you “vendor everything”. In other words, put following lines in Rails::Initializer.run block in your environment.rb.
config.load_paths += Dir["#{RAILS_ROOT}/vendor/gems/**"].map do |dir|
File.directory?(lib = "#{dir}/lib") ? lib : dir
end
If you don’t know what this is all about, go here.
IT IS VERY IMPORTANT that the binaries are for Ubuntu 8.04LTS (although it should work on 8.10, but I can’t guarantee it) and Mac OS X Leopard.
With this, you don’t have to have libxml-ruby gem installed on your machine. The same technique goes for any other gems that require compilation.