Archive for September, 2008

Ruby sum array values for currency

September 15th, 2008 | Category: Uncategorized

I found this handy trick to take an array built from some hash values and spit out a currency. Here is what I used it for. Inside my model, I built some test items.

def self.test_items
    [{:price => "5.50", :weight => 10, :item_description => "Test item 1"}, {:price => "7.75", :weight => 15, :item_description => "Test item 2"}, {:price => "9.95", :weight => 20, :item_description => "Test item 3"}]
end

I wanted to get a “Total Price” in the view, so I did the following. I created a method in my ApplicationHelper that looks like so

def total_for_cart(prices)
    number_to_currency(prices.inject(0){|sum, price| sum.to_f + price.to_f})
end

The above uses the Ruby inject method from the Enumerable class to simply add the float values. I’m also using the rails helper method number_to_currency for pretty formatting. Finally my view looks like this:

<p>Total: <%= total_for_cart(items.collect{|i| i[:price]}) %></p>

That simply collects the price from the items array and passes it to my helper method. Hope its helpful!

No comments

Ruby IRB Rails script/console tricks

September 10th, 2008 | Category: Development,Rails

I use script/console to play with any new features that I’m doing before they even make it into my app. I love using irb and script/console, but there were a few things that frustrated me.
1. Exit and reload the console when I make model changes
2. Command history was blown away on exit

So I decided to take some time to see if I could come up with solutions for the above. Luckily I found solutions for both of my problems.
Issue #1. After you make changes simply type “reload!” at the prompt to reload the app. This solved most of my problems, but still if I exit I lose all my previous commands.

Issue #2. Create the file ~/.irbrc to configure your irb settings. Include the following settings:

require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 200
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-history"

Voila, you now have a saved history just like a bash console. Use the “Up” and “Down” keys to browse your old history.

No comments

Ubuntu SVN cannot set LC_CTYPE locale

September 08th, 2008 | Category: Linux,Ubuntu

I ran into this error on a fresh Ubuntu Hardy server I set up hosting a SVN repository.  The error messages may look like the following:

 
svn: warning: cannot set LC_CTYPE locale
svn: warning: environment variable LANG is en_US.UTF-8
svn: warning: please check that your locale name is correct

This won’t cause any problems with the functionality of Subversion, but it gets annoying.  So the problem is that the LANG that is specified (in my case en_US.UTF-8) isn’t installed.  So, to install it do the following:

 
apt-get install language-pack-en-base

This will ask you if you want to install language-pack-en also, so say yes to the prompt.  Then you will be set.  If you language is something other than English “en”, then install the appropriate locale package.

4 comments