Ruby Method Name with Spaces
This is an introduction to Ruby metaprogramming where I cover two different ways to define a method. The idea actually started as a joke, but there are valuable lessons to be learned here.
Ruby Method Name with Spaces from Joon You on Vimeo.
What’s The Name of Method?
Someone asked me today how do I find out what method the current line of code is in during the runtime. Besides the obvious question, I nevertheless thought that it’s interesting that Ruby provides with that information.
Take a look at the code below.
The output is “hello”, which happens to be the name of the method that was called. If you do “__method__.inspect”, then you’ll get :hello instead of the String version.
Super Micro Web App – from my Rack presentation last year
__FILE__
What does the following get you?
Can you guess where the file is located?
The Latest Ruby
Are you brave enough to run the latest version of Ruby from trunk? Well, I do that on my development machine, because a) I lost my mind, or b) I’m the bravest man alive!
Well, all jokes aside, I thought it would be interesting to post how I do it. Obviously, I use Ubuntu to do this, usually in a virtual machine. However, this is not the case for my Lenovo X200. Before you do anything, make sure all the normal tools and libraries such as readline, zlib, ncurses, iconv, and etc are installed, especially the “dev” packages. In addition, run following:
Now that you’re armed and dangerous, let’s install by compiling as we normally do.
Just few notes/caveats:
- I’m assuming that you did not install Ruby yet, because this will override your existing installation.
- If you want to install it elsewhere, then change –prefix=/usr to somewhere else. I usually do “–prefix=/home/{your name}/.rubyhead” and remove “sudo” in make install. Then will change my $PATH.
- You may have to install openssl by “sudo apt-get install libssl-dev”
- Always go to “ext” directory and install each missing/broken extensions.
Passing Arguments to Rake, My Way
I’ve seen many interesting ways to pass arguments to rake tasks. The fact of matter is, I really don’t like dicking around with any constant or global variable. Here’s how I like to do it.
rake mytask:do_something setting:WHATEVER user:me
Using the argument facility built into Ruby, just create a hash inside of my task. I can even create a method to do this for all tasks. Below is my implementation.
I think it’s cleaner and easier way to do it, but that’s just my opinion. By the way, $* has an alias, ARGV.
Securing MongoDB
When I created the screencast series on MongoDB, I had no idea people would be so quick to put it into production. I thought I’d share my quick checklist on MongoDB in production environment to make sure it runs securely. While at it, take a look at this article.
1- Secure the host with ssh and make sure it’s not reachable from outside without going through a “gateway”. If you don’t know what this means, email me.
2- Use IPTables to lock down any access from unknown servers. link
3- Create an admin user and set authentication to admin – link
4- Set authentication to my dbs’s.
5- Change the port to something other than default 27017. I know, security through obsecurity.
Just remember that everything gets thrown out the window if you can log onto the server, including the authentication. That’s why you need to pay extra attention to securing it, not that it would be any different from any other servers.
Securing MongoDB is easy and simple. I’m sure there will be plenty of FUD around this.
Bad Ruby Practices: Commenting Out Code
Bad Ruby Practices: Commenting Out Code from Joon You.
Screencast: Add & Remove Methods in Ruby
Here’s my first installment of Stupid Rubyist Tricks Series.
Ruby provides metaprogramming feature that’s really powerful when used properly. Here, I show you the technique, but do something stupid so you don’t do it. Enjoy!
Adding & Removing Method in Ruby from Joon You.
Upgrading Your MongoDB
This morning, I got the notification that MongoDB 1.2.3 was released. Nice!
Unfortunately, I’ve been carrying around my Lenovo ThinkPad X200 running Ubuntu 9.10 since the New Year and completely forgot to update my MBP. It was still running 1.0! When you upgrade from very old version as I did, you’ll be greeted with an error message. What do you do? It’s dead simple, just do the following:
mongod --dbpath
That’s all there’s to it. You can go and upgrade every db files you have.