Posted: January 18th, 2010 | Filed under: Programming | No Comments »
Finally, I had some quality time with my laptop and got up to speed with recent developments in the Ruby / Rails world.
Many of you know how to use and do use several Ruby versions on the same machine, but for those who doesn’t, there are a couple of nice solutions. The first one is ruby_switcher and here’s a good post on how to install and use it. It’s really nice, easy to install and simple in use. The second (that I discovered) is RVM. It does all the same, but in a more organized and generalized way. The biggest advantage over the Ruby Switcher is that you can have multiple unconnected named Gem sets in every Ruby version. Say, you have several applications that you work on and to keep it all organized and to avoid Gem version collisions, you can use a named space for each of these projects. Very nice idea.
Last week I had an interview with one local agile team (you know I’m a freelancing type, but sometimes I feel we miss all the fun confining ourselves to home offices). The interview went well, in case you wonder, yet the office is inconveniently remote, so I back-pedaled a bit. During the meeting we accidentally taught one another a couple of tricks. I shared a thing or two about RSpec, and got some insights on HAML / SASS in return. It appears, there’s a project {less} which does what SASS does, but in a slightly different, more elegant fashion. It bases on top of the regular CSS and adds some cool new features, like variables, mix-ins, nested rules and operations. If you plan to use it, the first step would be the easiest. You grab your existing CSS files and start adding new features. It has a compiler to turn your files into legit CSS, and a plug-in that takes all the load for those who use Rails.
Yesterday I played with Rails 3 pre-release, but stumbled upon block after block. Partially, that’s because I used the all new Ruby 1.9.1, and partially because many plug-ins (especially for user authentication) still don’t work in Rails 3. Finally, I couldn’t make a single test run in Rails 3 + Ruby 1.9.1 combo due to TMail incompatibility with Ruby 1.9.1. Today I’m planning to try Ruby 1.8.7 and see how well that works. Rails 3 has so many great features and enhancements that it would be a shame to give up on them so quickly.
Until next time.
Posted: August 6th, 2009 | Filed under: Programming | Tags: edge, rails | No Comments »
Can’t help to share my joy. If you did this (see below) prior to current Rails Edge version when your time zone in environment.rb is set to anything but “UTF-8″, you did it wrong.
class Model < ActiveRecord::Base
named_scope ..., :conditions => { :date_time_field => Time.now }
end
Why? Because the Time.now will be taken in your server local time zone (set in environment.rb) and compared against the UTC saved in the database. To do it right, you needed this:
The same was applicable to finders and other stuff taking dates as arguments. Not very nice. I personally wrote patches to fix this in my projects.
Finally, in the latest Rails Edge it was fixed! No more kludge. Taken from the official post:
What time is it!?
Geoff Buesing provided a useful fix for Time, specifically when used in conjunction with ActiveRecord. Now you can save and search ActiveRecord objects using whichever local time zone you like, regardless of what your default time zone is configured for and everything now will just work. You no longer need to be concerned about converting your user’s local time into your default application time or vice versa.
Posted: August 6th, 2009 | Filed under: Programming | Tags: iPhone, objectivec | No Comments »
What if you followed all official (first, second, third) and unofficial tutorials to the letter, but still can’t make your tests run? Well, it’s what it was for me today. Here are two things I couldn’t find anywhere and had to figure myself:
- If you see only one Unit Test Bundle build error, and it’s says “Failed tests for architecture ‘i386′ (GC OFF)” (slight variations depending on your platform and the state of the GC flag), you don’t have any tests in the bundle. Yes, go check that counter in the Compile Sources task of your test target.
- If you added the application as the dependency to the test bundle, wrote the test that uses one of the classes and see the link error like below, you didn’t add any source files being tested to your test bundle for compilation. I know it’s insane to add them one by one, but it’s how they designed it. To make it a bit easier, right-click on the “Groups & Files” header, choose “Target Membership”, make sure your tests bundle is you current target and start checking missing sources.
".objc_class_name_XYZ", references from:
literal-pointer@__OBJC@__cls_refs@XYZ in XYZTests.o
sumbol(s) not found
collect2: Ld returned 1 exit status
Posted: August 6th, 2009 | Filed under: Programming, iPhone | Tags: iPhone, uinavigationcontroller, uitabbarcontroller | No Comments »
On iPhone, there’s the tab bar control that we, developers, can use to switch between several pages. It appears as a black stripe with icons at the bottom of the screen. Probably, the most famous is the tab bar of the Clock application:

It can show up to 5 icons and if there’s not enough space left, the last one is replaced with the “More …” item. When clicked, it shows the nice view with the list of all available options that didn’t fit into the tab bar. UITabBarController delegates the job to the UINavigationController. Now why I tell all this?
By default, the UINavigationController uses that standard blue top bar, but what if I want it to be Black as in the rest of my app?
First, let’s change the style of the “More …” page top bar. It’s all pretty straight forward as you can see. Just get the navigation controller and set the style of its bar:
tabBarController.moreNavigationController.navigationBar.barStyle = UIBarStyleBlack;
Finally, let’s change the style of the bar on the “More …” / Edit page (that says “Configure” by the way). It appears that you can’t drill down into the object model like above, but have to catch the moment when the editor is about to come out and do the magic. For this to happen, you needthe tab bar delegate (UITabBarControllerDelegate protocol) and use the tabBarController:willBeginCustomizingViewControllers: callback to nail it:
tabBarController.delegate = self;
and later:
- (void)tabBarController:(UITabBarController *)tabBarController
willBeginCustomizingViewControllers:(NSArray *)viewControllers {
UIView *views = [tabBarController.view.subviews objectAtIndex:1];
UINavigationBar *navBar = [[views subviews] objectAtIndex:0];
navBar.barStyle = UIBarStyleBlack;
}
In the above 3-liner, you can see that we do the trick to actually find the navigation bar. It works nicely on iPhone 3.0, but may change in the future, so be careful.
Don’t forget that you can always subclass UITabBarController and hide all this customization logic inside to stay on the clean design side. In my work, I do that and used the tabBarController here to clearly state what model we operate on.
That’s all, folks. Hope it helped someone to save a couple of hours.
Posted: August 2nd, 2009 | Filed under: Programming | Tags: bash, textmate, Tips | No Comments »
There’s a couple of Bash patterns I often use in my scripting that I would love to share. Both of them are immediately useful in your TextMate practice through commands, and that’s what makes the study really fun.
Default Values
Assuming that you want a user to give you the path as the first argument and fall back to something if he didn’t, normally you may have done this:
if [ -z $1 ]
then
MY_PATH=/usr/local
else
MY_PATH=$1
fi
And here’s how you do it geeky Bash-style:
DEFAULT_PATH=/usr/local
MY_PATH=${1:-$DEFAULT_PATH}
Shorter and delivers the message way more transparently, doesn’t it? That minus sign before the $DEFAULT_PATH is critical, so don’t miss it. Which leads us to the next tip.
String Splitting
If you need only some part of the string, you can easily cut it out knowing the start/end indexes or just one of them.
VALUE=abcd
INDEX=2
# Gives 'ab'
LEFT_FROM_INDEX=${VALUE:0:INDEX}
# Gives 'cd'
RIGHT_FROM_INDEX=${VALUE:INDEX}
What about TextMate?
Now that you possess the secret knowledge, you can use it in your TextMate commands. One example scenario mentioned all around is replicating the common pattern where the command operates either on the selection or the current word. Here’s how it would look:
${TM_SELECTED_TEXT:-$TM_CURRENT_WORD}
Note the order of the parameters, TM_CURRENT_WORD is the default, not the other way around. If there’s nothing selected, it will come in handy. Now, if you read my previous posts on Ruby / Shell code execution, this should ring a bell. Here’s how the selection-or-line logic would look:
${TM_SELECTED_TEXT:-$TM_CURRENT_LINE}
Amazingly simple and ultimately useful!
Posted: August 1st, 2009 | Filed under: Programming | Tags: cgi, ruby | No Comments »
I’m a big fan of Ruby, and still every time I need some simple server-side functionality — you know those go there, take that sort of tasks — I have to revert to good old PHP. It’s obvious that building a full-blown Rails app is an overkill.
This morning I need another tiny little script to parse the HTML page and get some info back in JSON. And all this for a static web-site, so that it can’t be hosted locally. So what do I do? Getting back to roots…
Many web servers let you run CGI scripts, which is nothing else but an ordinary script that is executed by an interpreter of your choice and the results are thrown back as the response. Of course, it’s a bit more complicated that that, but the gist is accurate, and what’s more important is it’s hilariously easy to write.
If you happen to have a site with CGI enabled, you can try it right away. If not, well, check the hosting docs, or your web server configuration to see if there’s anything you can do. Most often, you will find the cgi-bin directory where all magic happens sitting next to public_html or www in your web site location root. Don’t forget that everything you put into cgi-bin has to be executable by the web server. It means that either you need to give it the correct ownership and the owner execution permissions or simply let everyone run it (which is dangerous sometimes, but good enough for a couple of tests).
So, here’s what you can try throwing into /cgi-bin/ruby.cgi:
#!/usr/bin/ruby
puts 'Content-type: text/plain'
puts
puts 'Test Complete'
Now when you run it by entering http://your_site/cgi-bin/ruby.cgi in the address line, you will get the nice and shiny “Test Complete” string.
Note that content type declaration and the empty line after it. Two things are important about these:
- Both lines are mandatory
- You can set any content type you like, not just output things in plain text. It can be JSON, XML, HTML or GIF, PNG, PDF — anything you can think of.
Now why don’t you throw a couple of gems into the mix and build something cool, fast and breathing!
Posted: August 1st, 2009 | Filed under: Programming | Tags: ruby, textmate | 4 Comments »
In one of my previous posts I mentioned how nice it is to execute Ruby code and get the results in comments. This is quite useful when you build documentation or quickly test some ideas.
There’s one other method of doing this which is based on the Textmate’s ability to execute the code line at cursor or the selected piece of text. It’s as easy as it sounds.
- First, enter the piece of Ruby code to execute, having in mind that the result of the last line will be output below:
- Now select all three lines and press ⌃⇧E (Execute line as Ruby). The result will appear on the next line
One neat trick is that you can use printf(…) function to format your output and, in this case, the result of the last line won’t be displayed, but the printed output will.
I find it useful when there’s something to try right inside the document or there’s some formula to solve. It lets me stay where I am and not to open a separate document just for the Ruby snippet.
As a little bonus, I just want to mention that you can run unix commands in the same fashion and get the output right in your document. For example:
- Enter the following line and leave the cursor at the end of it:
- Press ⌃R (Text / Execute Line Inserting Result) to execute it. The result will be placed below.
The list of all nginx processes will be dumped. Sweet, ha.
Posted: July 29th, 2009 | Filed under: Programming | Tags: config, ssh | No Comments »
Many people around me don’t know about the magical ~/.ssh/config file. If you are among of them, just read on and pretend you knew it all the way.
Here’s a simple, yet useful sample that shows just the two most essential features that you can start using today:
Host someplace.com
Port 22222
Host 174.10.20.30
IdentityFile ~/.ssh/id_rsa.office
What do we see here?
- ~/.ssh/config is a plain-text file. Note that it’s file permissions should be “600″ (read/write by owner only), and ~/.ssh directory permissions must be “700″. It is important. If they are too permissive, SSH won’t trust your configuration.
- The file consists of sections that start with the “Host” declaration. Each section corresponds to one host specified either by name or IP address.
- Statements in the sections are indented (traditionally with 3 spaces).
- “Port” statement suggests the port number to use when connecting to the given Host.
- “IdentityFile” statement lets you use a non-default (id_rsa) private key. I see you burst into tears. Yes, you can have many keys for different occasions — personal and team; any number of them.
These are just few options that I find the most useful in my everyday practice, but feel free to explore it deeper.
One important thing I need to mention is that this configuration applies to absolutely all cases when you open the SSH connection by standard means, not just when SSH’ing somewhere directly. You can do SCP to transfer files, access remote repositories with SVN and GIT, deploy your developments with Capistrano and much more.
Have fun!
Posted: July 28th, 2009 | Filed under: Programming | Tags: irb, ruby, textmate | 3 Comments »
In my day-to-day development I keep IRB handy just to sketch some statement or check how some method works with a given data set. It’s all quick and helpful, but working in TextMate, I always forget about it also has a tiny small feature that lets us do the same in much more comfortable fashion. The next time I need to check something, I will:
- Open an empty text document
- Set type to Ruby with ⌃⌥⇧R
- Enter my ruby code just like this:
a = [1, 2, 3]
b = [2, 3]
a + b # =>
a - b # =>
- Finally, run it with ⌃⌘⇧E and get this as the result:
a = [1, 2, 3]
b = [2, 3]
a + b # => [1, 2, 3, 2, 3]
a - b # => [1]
See? Handy.
By the way, I may have noticed that this format is frequently used in Ruby docs. Now you know how do they do it, right?
Posted: July 27th, 2009 | Filed under: Programming | Tags: gems, plugins, rails | No Comments »
To keep up with the quickly evolving Rails community I constantly read many blogs and watch podcasts. Here are some curious new gems I discovered recently.
Rack::Bug. A comprehensive debugging plugin for your Rails applications that sits on the Rack middleware level and captures a ton of curious info ranging from request parameters and Rails environment to the request processing timings, SQL execution details and much more. It’s not suitable for the older Rails-based projects as it needs the Rack which has become the part of Rails (or Rails was adapted to run in the Rack stack to be precise) just recently, but for new projects it can be a life saver.
FiveRuns TuneUp. While still in beta, this plugin and their server-side service may give a good quick insight into what’s going on under the hood of the request processing. It sits at the top of the page as a bar and gives a nice JavaScript popup with the response rendering tree and model schemas. I noticed a some quirks and wasn’t able to dig deep enough to nail a couple of problems I noticed, but it’s a great general-purpose tool to quickly see who’s the greediest of them all.
NewRelic RPM. Not new, but for the completeness sake. A great paid (with a free plan) service with the app-side agent plugin for performance data collection. Collects ALL possible data about your app down to the execution plans of your statements and error trace-backs, but costs a fortune. None of my clients went higher than a Bronze plan because of their cosmic prices. I’m sure they have their customers, it’s just not for everyone. Still, thanks for NewRelic guys (honest thanks), there’s a way to get access to all info if you have a head, hands and some time.
Metric_fu. All-in-one code analysis package grouping several other tools and providing a unified web interface with graphs and other niceties. Not much more to say here. Useful as hell if you on the quality side of the application development business.
Whenever. Quoting the author, “Whenever is a Ruby gem that provides a clear syntax for defining cron jobs”. This is one of an awesome piece of software. Rare Rails app these days needs no background / periodical processing. Even simple news e-mail delivery is done with cron + rake or similar solutions. This tool lets you keep your cron jobs definitions in ruby and with a tiny bit of an effort, you can add the crontab regeneration to your deployment process. No need to handle all these crontabs manually any more.
That’s all folks. However, this brings up an interesting question. How do you discover what new tools / gems emerge in the Rails realm these days? It develops insanely rapidly.